class summary_model(models.Model):
username = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT)
summary=models.TextField(unique=False, blank=False, null=False)
numberofprojects=models.CharField(max_length=264, unique=False, blank=False, null=False)
numberofinternships=models.CharField(max_length=264, unique=False, blank=False, null=False)
numberofjobs=models.CharField(max_length=264, unique=False, blank=False, null=False)
def __str__(self):
return str(self.username)
class summary_form(forms.ModelForm):
#fields from model
class Meta:
model = summary_model
fields = ('summary','numberofprojects','numberofinternships','numberofjobs')
def summaryview(request):
username=request.user
if request.method == 'GET':
summaryform=summary_form(request.GET or None,instance=summary_model.objects.get(username=username))
print(summaryform)
elif request.method == 'POST':
form4=summary_form(request.POST or None,instance=summary_model.objects.get(username=username))
if form4.is_valid():
summary_obj = form4.save(commit=False)
summary_obj.username = request.user
summary_obj.save()
return redirect('#anotherview')
else:
summaryform=summary_form(instance =summary_model.objects.get(username =username))
return render(request,'app/summary.html',{'summaryform':summary_form,})
{% block content %}
<form action="" method="post" novalidate>
{% csrf_token %}
{{ summaryform.non_field_errors }}
<div class="row form-row bg-white has-shadow">
<div class="col-12">
<span class="labelspan">{{ summaryform.summary.label }}</span>
{{ summaryform.summary.errors }}
<span class="inputfieldspan">{{ summaryform.summary}}</span>
</div>
<div class="col-6">
<span class="labelspan">{{ summaryform.numberofprojects.label }}</span>
{{ summaryform.numberofprojects.errors }}
<span class="inputfieldspan">{{ summaryform.numberofprojects}}</span>
</div>
<div class="col-6">
<span class="labelspan">{{ summaryform.numberofinternships.label }}</span>
{{ summaryform.numberofinternships.errors }}
<span class="inputfieldspan">{{ summaryform.numberofinternships}}</span>
</div>
<div class="col-6">
<span class="labelspan">{{ summaryform.numberofjobs.label }}</span>
{{ summaryform.numberofjobs.errors }}
<span class="inputfieldspan">{{ summaryform.numberofjobs}}</span>
</div>
</div>
{{ summaryform.message.help_text }}
<button type="submit" class="btn3">Save and Continue</button></form>
{% endblock %}
我需要以摘要形式进行输入,并允许用户使用相同的视图进行编辑。当我保存填充数据时,其更新内容是主要问题是它没有使用实例从数据库中填充数据。
答案 0 :(得分:0)
尝试 字段= [“摘要”,“项目数”,“实习数”,“工作数”] 成forms.py