我有一个基本表单,它接受模板中的文件/图像。由于某种原因,它不会验证,我看不到错误。
的 views.py 的
# Flag a Job as complete
@login_required()
@user_passes_test(lambda u: u.groups.filter(name='Developer').exists(), login_url='/login/', redirect_field_name='not allowed')
@require_http_methods(['POST'])
def job_complete(request, jobid, userid):
# Get the Job
job = Jobs.objects.get(id=jobid)
jobsubmit = JobSubmitForm(request.FILES)
if jobsubmit.is_valid():
js = jobsubmit.save(commit=False)
js.user_id = userid
js.job_id = jobid
js.save()
job.status = 'IR'
job.save()
return redirect('view_job', jobid=jobid, slug=job.slug)
else:
messages.error(request, "There are form errors!")
return redirect('view_job', jobid=jobid, slug=job.slug)
的 forms.py 的
class JobSubmitForm(forms.ModelForm):
class Meta:
model = JobSubmit
fields = ('file', 'image', 'comment', 'gitHubLink')
def save(self, commit=True):
jobsubmit = super(JobSubmitForm, self).save(commit=False)
jobsubmit.user_id = self.cleaned_data['user_id']
jobsubmit.job_id = self.cleaned_data['job_id']
if commit:
jobsubmit.save()
return jobsubmit
的 view.html 的
<form method="post" action="/job/job_complete/j{{ job.id }}/u{{ request.user.id }}/" class="form-inline btn-group" enctype="multipart/form-data">
{% csrf_token %}
<div class="span6 inline">
<label class="control-label">Attach Files: </label>{{ job_submit.file }}
<p class="help-block">Attach files that go with this Job.</p>
</div>
<div class="span6 inline">
<label class="control-label">Attach Images: </label>{{ job_submit.image }}
<p class="help-block">Attach images that go with this Job.</p>
</div>
<div class="span6 inline">
<label class="control-label">GitHub URL: </label>{{ job_submit.gitHubLink|add_class:"form-control"|attr:"placeholder:Example: https://www.github.com/path/to/code/repo/or/commit" }}
<p class="help-block">If hosting work on GitHub.</p>
</div>
<div class="span6 inline"><label class="control-label">Job Comments(Required): </label>{{ job_submit.comment|add_class:"form-control"|attr:"placeholder:Example: Fun Job! Please keep me in mind for future work!" }} </div>
<div class="modal-footer">
<button type="submit" class="btn btn-success btn-med pull-left"><i class="fa fa-check-circle"></i> Job Complete</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</form>
的 models.py 的
# Store data related to a Job (files, comments, etc.)
class JobSubmit(models.Model):
job = models.ForeignKey(Jobs)
user = models.ForeignKey(User)
file = models.FileField(upload_to="uploads/jobs/files", blank=True, null=True)
image = models.ImageField(upload_to="uploads/jobs/images", blank=True, null=True)
comment = models.TextField()
gitHubLink = models.URLField(blank=True)
希望这不是什么傻事......这是漫长的一天,困倦的编程不是最好的主意。 :/
如果有人发现错误,请欣赏帮助。指针也受到欢迎。欢呼声,
答案 0 :(得分:1)
这一行:
jobsubmit = JobSubmitForm(request.FILES)
应该是:
jobsubmit = JobSubmitForm(request.POST, request.FILES)
试试吗?