【发布时间】:2019-09-02 09:30:50
【问题描述】:
我发现很难在 html 模板中显示 django 表单。 django 表单无法在模板中呈现。我正在使用类基础视图,下面是我的 views.py、urls.py、models.py 和 html 模板的代码:
views.py
class Home(CreateView):
models = Blog
queryset = Blog.objects.filter(publish = True)
template_name='index.html'
fields = '__all__'
urls.py
urlpatterns=[
path('', Home.as_view(), name='index'),
]
models.py
Continent = (
('select continent', 'Select Continent'),
('africa', 'Africa'),
('europe', 'Europe'),
('north america', 'North America'),
('south america', 'South America'),
('asia', 'Asia'),
('australia', 'Australia'),
('Antarctica', 'Antarctica'),
)
class Blog(models.Model):
name= models.CharField(max_length = 200)
company= models.CharField(max_length = 200)
post = models.CharField(max_length = 200)
author= models.ForeignKey('auth.User', on_delete = models.PROTECT)
mantra= models.CharField(max_length = 200, help_text='make it short
and precise')
continent = models.CharField(choices= Continent, default= 'select
continent',
help_text='help us to know you even more',
max_length=50)
publish = models.BooleanField(default =True)
def __str__(self):
return self.name
def get_absolute_url(self): # new
return reverse('post_detail')
index.html
{% extends "base.html" %}
{% load static %}
{% block content %}
<body class="loading">
<div id="wrapper">
<div id="bg"></div>
<div id="overlay"></div>
<div id="main">
{{form.as_p}}
{% include "partials/_footer.html" %}
</div>
</div>
</body>
{% endblock %}
我们将不胜感激。谢谢。
【问题讨论】:
-
这是
model = Blog不是models = Blog -
@dirkgroten:我有点惊讶 Django 没有从
queryset派生模型。因为可以使用some_queryset.model访问它。 -
@WillemVanOnsem 是真的。刚刚也意识到了这一点。它应该,因为
model is None能够从queryset获得它。所以问题出在其他地方。 -
您能在浏览器中查看您网页的源代码吗?你真的看到
<div id="main">了吗? -
感谢大家的及时回复。我确实按照 dirkgroten 的建议将模型更改为模型;但问题仍然相同。我也可以在我的浏览器源代码中看到
标签: django django-forms