如何解决mutli-select没有值时,单击提交按钮时,Django表单未提交
我正在使用django-taggit库来表示“问题”字段。在模型中将其设置为blank = True。当我尝试提交此表单时,如果在多选字段中选择了一个或多个问题,则效果很好,但如果该字段未选择任何内容,则不会提交。问题在于它甚至不会引发错误或未在视图代码中出现断点,因此我无法对其进行调试以找出问题所在。有什么想法吗?
Django Form类:
class CreateJob(ModelForm):
def __init__(self,*args,**kwargs):
selected_questions = kwargs.pop('selected_questions')
super(CreateJob,self).__init__(*args,**kwargs)
self.initial['questions'] = selected_questions
questions = ModelMultipleChoiceField(queryset=Tag.objects.all())
class Meta:
model = Job
fields = ('name','description','skills_required','submission_deadline','participation_ends','tagged_freelancers','assigned_freelancer','questions')
widgets = {
'submission_deadline': DateInput(attrs={'data-provide': 'datepicker'}),'participation_ends': DateInput(attrs={'data-provide': 'datepicker'})
}
Django视图:
def job_edit(request,job_id,template_name='projects/job_create.html'):
job = get_object_or_404(Job,pk=job_id)
job_form = CreateJob(request.POST or None,instance=job,selected_questions=job.questions.all())
if job_form.is_valid():
job_form.save()
return redirect('job_board')
return render(request,template_name,{'job_form': job_form,'is_new': False})
HTML模板:
<div class="container">
<div class="row justify-content-sm-center">
<div class="col-sm-8">
<div class="mb-3 text-left">
{% if is_new %}
<h1>New Job</h1>
{% else %}
<h1>Update Job</h1>
{% endif %}
</div>
<form method="post" class="form">
{% csrf_token %}
{% bootstrap_field job_form.name %}
{% bootstrap_field job_form.description %}
{% bootstrap_field job_form.skills_required %}
{% bootstrap_field job_form.submission_deadline %}
{% bootstrap_field job_form.participation_ends %}
{% bootstrap_field job_form.tagged_freelancers %}
{% bootstrap_field job_form.assigned_freelancer %}
{% bootstrap_field job_form.questions %}
{% bootstrap_button "Submit" button_type="submit" button_class="btn-secondary" %}
</form>
</div>
</div>
</div>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。