文件不会从Django中的Web表单上传

时间:2021-11-29 21:17:21

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.

在本地运行捆绑服务器进行开发,我可以通过前端和管理界面上的Web表单成功上传文件。在远程服务器上运行它(带有mod_python的Apache)我可以通过管理界面成功上传文件,但尝试通过Web前端不会产生上传文件。

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?

我已将FILE_UPLOAD_PERMISSIONS = 0644添加到设置中,检查了两个设置文件,并查找了其他地方描述的类似问题。图我要么忘了一个设置,要么需要以不同的方式解决这个问题。有什么建议么?

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))

2 个解决方案

#1


23  

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:

您不显示模板。如果我不得不猜测,看到上传通过管理界面工作,我会说你忘了把enctype放在你的表格标签中:

<form enctype="multipart/form-data" method="post" action="/foo/">

#2


4  

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

首先,您是否确定您的模板中包含enctype =“multipart / form-data”标志?

<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.

首先,不需要在ModelForm中覆盖save(),因为你没有在其中做任何额外的工作。

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

其次,不需要存储new_application变量,只需调用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.

第三,您应该在JobOpening模型中使用slug字段并将其传递给查询字符串。记住,这不是PHP,使用像/ jobs / opening / my-cool-job-opening /这样的漂亮网址,这就是slu for的用途;独特的人类可读网址。您视图中的GET代码非常脆弱。

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.

最后,您可能希望使用render_to_response快捷方式功能,因为它将节省您必须详细调用模板加载器,创建上下文并手动呈现它们。

#1


23  

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:

您不显示模板。如果我不得不猜测,看到上传通过管理界面工作,我会说你忘了把enctype放在你的表格标签中:

<form enctype="multipart/form-data" method="post" action="/foo/">

#2


4  

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

首先,您是否确定您的模板中包含enctype =“multipart / form-data”标志?

<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.

首先,不需要在ModelForm中覆盖save(),因为你没有在其中做任何额外的工作。

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

其次,不需要存储new_application变量,只需调用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.

第三,您应该在JobOpening模型中使用slug字段并将其传递给查询字符串。记住,这不是PHP,使用像/ jobs / opening / my-cool-job-opening /这样的漂亮网址,这就是slu for的用途;独特的人类可读网址。您视图中的GET代码非常脆弱。

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.

最后,您可能希望使用render_to_response快捷方式功能,因为它将节省您必须详细调用模板加载器,创建上下文并手动呈现它们。