我使用窗体将数据保存到它。Django的:保存URL请求参数到表单
models.py
class presciptiontemplates(models.Model):
templateid = models.AutoField(primary_key=True)
template = tinymce_models.HTMLField()
draft = models.BooleanField(default=False)
savedate = models.DateTimeField(default=datetime.now())
patientid = models.ForeignKey('Patient')
我传递的参数患者ID从URL。
url(r'^addprescription/(?P<patid>\d+)/$', views.viewtemplate
我怎么这个参数保存到形式
views.py
def viewtemplate(request, patid):
userid = patid
form = templateform(request.POST)
if request.method == "POST":
if form.is_valid():
presciptiontemplates.patientid = userid
form.save()
return redirect('index')
else:
form = templateform()
return render(request, 'presapp/prescription.html', {'form': form})
forms.py
class templateform(forms.ModelForm):
class Meta:
model = presciptiontemplates
fields = ['template', 'draft']
widgets = {
'template': TinyMCE(),
'draft': forms.CheckboxInput()
}
labels = {
"patientid": _("Patient ID"),
}
def __init__(self, *args, **kwargs):
super(ThatForm, self).__init__(*args, **kwargs)
self.fields['template'].required = True
这给了我一个错误
[微软] [SQL Server本机客户端11.0] [SQL Server]不能插入 NULL值插入列 'patientid'
Django 1.10.4
Python 3.5.2
Windows 7
这个问题有很多错误。你的模型在哪里?什么是缩进。什么是似乎未定义的presciptiontemplates? – e4c5
显示prescription.html并告诉我们什么是** presciptiontemplates **? –
新增的ModelForm,presciptiontemplates是我打算从富文本编辑器 –