[英]Django Form, User input fields not rendering in template
Django newb here, but I'm learning. Django newb在这里,但我正在学习。
Problem Statement 问题陈述
Input fields not rendering in html template. 输入字段未在html模板中呈现。
Output in Browser 在浏览器中输出
True 真正
| | Submit Button |
提交按钮|
Relevant Code 相关代码
forms.py
from django import forms
from django.db import models
class PostNotes(forms.Form):
clientid = models.IntegerField()
notetext = models.CharField(max_length=1000)
- --
views.py
def post_form_notes(request):
if request.method == 'GET':
sawit = True
form = PostNotes(initial={'notetext':'This is a sample note'})
else:
sawit = False
pass
return render(request, 'clientadmin/post_form_notes.html', {
'form': form,
'sawit': sawit,
})
- --
post_form_notes.html
{{ sawit }}
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Submit" />
</form>
Troubleshooting Already Done 完成故障排除
I modified the forms.py to use the model I already had for the DB. 我修改了forms.py,以使用数据库已有的模型。
forms.py: forms.py:
from django.forms import ModelForm
from clientadmin.models import notes
class PostNotes(ModelForm):
class Meta:
model = notes
fields = ['notedate', 'notetext']
I also modified the views.py to not set an initial value, so the function uses the following instead of what was asked. 我还修改了views.py,使其不设置初始值,因此该函数使用以下内容代替询问的内容。
models.py: models.py:
def post_form_notes(request):
if request.method == 'GET':
form = PostNotes()
else:
pass
return render(request, 'clientadmin/post_form_notes.html', {
'form': form,
})
Hope this helps someone that was having the same problem I was... 希望这对遇到同样问题的人有所帮助...
Reference the following URL for further information: https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/ 请参考以下URL以获取更多信息: https : //docs.djangoproject.com/en/1.10/topics/forms/modelforms/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.