我正在创建模型形式。我已经设法从表中提取我要使用的字段(使用postgresql):
表格:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul class="render-menu">
<li class="font-size">list 1</li>
<li class="font-size">list 2</li>
<li class="font-size">list 3</li>
<li class="font-size">list 4</li>
</ul>
表格:
class Response(models.Model):
Question = models.ForeignKey(Question, on_delete=models.CASCADE)
Topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
Response = models.TextField()
Client = models.ForeignKey(ClientDetail, on_delete=models.CASCADE)
Planit_location = models.ForeignKey(Planit_location, on_delete=models.CASCADE)
def __str__(self):
return self.Response
我现在想在Planit_locaiton字段中添加,但是当我这样做时,出现标题中所述的光标错误。不知道为什么。这是位置表:
class ResponseForm(forms.ModelForm):
class Meta:
model = Response
fields = ('Response', 'Topic', 'Client')
widgets = {
'Response': Textarea(attrs={'rows':3, 'cols':200}),
}
HTML:
class Planit_location(models.Model):
Planit_location = models.CharField(max_length=255)
def __str__(self):
return self.Planit_location
views.py:
<form method="post" name="uploadform" style="margin-left: 16px">
{% csrf_token %}
<h4>Add Data</h4>
<table>
{{ qform.as_table }}
{{ rform.as_table }}
</table>
<button type="submit" class="btn btn-default">Submit</button>
</form>
编辑
跟踪:
def adddata(request):
if request.user.is_authenticated:
username = request.user.username
if request.method == 'POST':
qform = QuestionForm(request.POST)
rform = ResponseForm(request.POST)
if qform.is_valid() and rform.is_valid():
qf = qform.save()
rf = rform.save(commit=False)
rf.Question = qf
rf.save()
return render(request, 'app/adddatatest.html', {
"qform": QuestionForm(),
"rform": ResponseForm(),
"username": username,
})
else:
qform = QuestionForm()
rform = ResponseForm()
return render(request, 'app/adddatatest.html', {
"qform": QuestionForm(),
"rform": ResponseForm(),
"username": username,
})