English 中文(简体)
File does not upload from web form in Django
原标题:

Howdy - I ve written a very simple app to accept job applications including a resume upload.

Running the bundled server for development locally, I can successfully upload files via the web form on the front end and the admin interface. Running it on the remote server (Apache with mod_python) I can successfully upload files via the admin interface but attempts over the web front end yield no uploaded file.

I ve added FILE_UPLOAD_PERMISSIONS = 0644 to settings, checked the two settings files, and looked for similar problems described elsewhere. Figure I m either forgetting a setting or need to go about this a different way. Any suggestions?

For reference, code included.

The model:

class Application(models.Model):
    job = models.ForeignKey( JobOpening )
    name = models.CharField(max_length=100)
    email = models.EmailField()
    date_applied = models.DateField()
    cover_letter = models.TextField()
    resume = models.FileField(upload_to= job_applications , blank=True)

    def __str__(self):
        return self.name

    def save(self):
        if not self.date_applied:
            self.date_applied = datetime.today
        super(Application, self).save()

The form:

class JobApplicationForm(ModelForm):    
    class Meta:
        model = Application

    def save(self, commit=True, fail_silently=False):
        super(JobApplicationForm, self).save(commit)

The view:

def job_application(request):
    ajax = request.GET.has_key( ajax )
    if request.method ==  POST :
        form = JobApplicationForm(request.POST, request.FILES)
        if form.is_valid():
            new_application = form.save()
            return HttpResponseRedirect( /about/employment/apply/sent/ )
    elif request.GET.has_key( job ):
        job = request.GET[ job ]
        form = JobApplicationForm({ job : job})
    else:
        return HttpResponseRedirect( /about/ )
    t = loader.get_template( employment/job_application.html )
    c = Context({
         form : form,
    })
    return HttpResponse(t.render(c))
最佳回答

You don t show the template. If I had to guess, seeing as the upload works via the admin interface, I d say you ve forgotten to put the enctype in your form tag:

<form enctype="multipart/form-data" method="post" action="/foo/">
问题回答

First, Have you made sure your template has the enctype="multipart/form-data" flag in it?

<form action="." method="POST" enctype="multipart/form-data">
    ...
</form>

First, there s no need to override save() in your ModelForm since you re not doing any extra work in it.

Second, there s no need to store the new_application variable, simply call form.save().

Third, you should be using a slug field in your JobOpening model and passing that in the querystring. Remember, this isn t PHP, use pretty urls like /jobs/opening/my-cool-job-opening/, that s what slugs are for; unique human readable urls. Your GET code in your view is very fragile as it stands.

Finally, you may want to use the render_to_response shortcut function as it will save you having to verbosely call template loaders, create context and render them manually.





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签