我无法在表单的cleaned_data下获取字段名称,因为该字段位于要请求的数据下面。POST
views.py
def newProjectView(request):
if request.method == 'POST':
projectForm = ProjectInsertionForm(request.POST)
if projectForm.is_valid():
jobDesc = projectForm.save(commit=False)
jobDesc.save()
messages.success(request, message="Succefully saved the Project.")
return redirect('accounts:profile')
else:
messages.error(request, message='Unable to save the project, Please do check the details. \n%s \n%s' % (projectForm.cleaned_data, projectForm.data))
return redirect('accounts:profile')
forms.py
class ProjectInsertionForm(forms.ModelForm):
class Meta:
model = CompanyDesc
fields = ('companyDets', 'title', 'details')
models.py
class CompanyDesc(models.Model):
companyDets = models.ForeignKey(CompanyList, on_delete=models.CASCADE)
details = models.TextField()
title = models.CharField(max_length=200)
def __str__(self):
return self.title
work.html
<p>
<form action="{% url 'accounts:newProject' %}" method="POST">
{% csrf_token %}
<p><input type="text" name="companyDets" id="id_companyDets" value="{{ company.company }}" class="col-md-12" style="border-radius: 5px; border: 1px solid #d32878; padding: 5px;"></p>
<p><input type="text" name="title" id="id_title" class="col-md-12" style="border-radius: 5px; border: 1px solid #d32878; padding: 5px;"></p>
<p><input type="text" name="details" id="id_details" class="col-md-12" style="border-radius: 5px; border: 1px solid #d32878; padding: 5px;"></p>
<p><input type="submit" class="razo-btn" value="Save Job"></p>
</form>
</p>
在此示例中,标题和详细信息显示在已清除的数据下,但companyDets属性不在已清除的数据中
最后在视图中,form.is_valid()返回False并转到else块
我做错什么了吗??
答案 0 :(得分:0)
在型号中:
companyDets = models.ForeignKey(CompanyList, on_delete=models.CASCADE, related_name='foo')
在html中:
<p><input type="text" name="foo" id="id_companyDets">
尝试
答案 1 :(得分:0)
您必须更改表格。 表格
Class ProjectInsertionForm(forms.Form):
companyDets = forms.CharField(max_length=202)
title = forms.CharField(max_length=202)
details = forms.CharField(max_length=202)
观看次数
if form.is_valid():
foo1 = form.cleaned_data.get("companyDets")
foo2 = form.cleaned_data.get("title")
foo3 = form.cleaned_data.get("details")
obj = CompanyList.objects.get(***whatever field is in CompanyList Modal*** = foo1)
obj2 = CompanyDesc(companyDets = obj, details = foo2, title=foo3)
obj2.save()
messages.success(request, message="Succefully saved the Project.")
return redirect('accounts:profile')
html
<p>
<form action="{% url 'accounts:newProject' %}" method="POST">
{% csrf_token %}
<p><input type="text" name="companyDets" id="id_companyDets" class="col-md-12" style="border-radius: 5px; border: 1px solid #d32878; padding: 5px;"></p>
<p><input type="text" name="title" id="id_title" class="col-md-12" style="border-radius: 5px; border: 1px solid #d32878; padding: 5px;"></p>
<p><input type="text" name="details" id="id_details" class="col-md-12" style="border-radius: 5px; border: 1px solid #d32878; padding: 5px;"></p>
<p><input type="submit" class="razo-btn" value="Save Job"></p>
</form>
</p>