【发布时间】:2016-08-15 18:50:39
【问题描述】:
这里是 Django 新手,但我正在学习。
问题陈述
输入字段未在 html 模板中呈现。
在浏览器中输出
是的
|提交按钮 |
相关代码
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>
已完成故障排除
- 我已经撤回了相当多的代码(特别是如果我看到 POST) 来自浏览器的请求。不用找了。
- 我还包含了一个变量,以确保我看到了 GET 请求,并且模板变量也正常工作。我得到 True 的输出。
- 尽可能简化表单类。
【问题讨论】:
-
马特,感谢您的回复,我认为您对正在发生的事情是正确的。我创建一个对象并将其传递给模板。我相信,|as_p 过滤器应该将其转换为对象中的代表对象。但是,由于它不起作用,我当然不确定这是正确的。遵循(或多或少)这个例子。 pythoncentral.io/how-to-use-python-django-forms