文件不会从Django中的Web表单上传 [英] File does not upload from web form in Django
问题描述
在本地运行捆绑式服务器进行开发,我可以成功上传文件通过前端的Web表单和管理界面。在远程服务器上运行(Apache with mod_python)我可以通过管理界面成功上传文件,但尝试通过Web前端没有上传的文件。
将 FILE_UPLOAD_PERMISSIONS = 0644
添加到设置中,检查两个设置文件,并查找其他地方描述的类似问题。图我要么忘记一个设置,要么需要这样做。任何建议?
为了参考,包含代码。
模型:
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()
形式:
类JobApplicationForm(ModelForm):
类Meta:
模型=应用程序
def save(self,commit = True,fail_silently = False):
super(JobApplicationForm,self).save(commit)
视图:
def job_application(request):
ajax = request .GET.has_key('ajax')
如果request.method =='POST':
form = JobApplicationForm(request.POST,request.FILES)
如果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,
})
返回HttpResponse(t.render(c))
您不显示模板。如果我不得不猜测,看到上传通过管理界面工作,我会说你忘记把这个enctype放在你的表单标签中:
< form enctype =multipart / form-datamethod =postaction =/ foo />
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/">
这篇关于文件不会从Django中的Web表单上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!