【发布时间】:2021-10-25 00:41:48
【问题描述】:
我正在为发布博客制作一个网络应用程序。我添加了类别字段来标识该博客的类别。但问题是我只能在管理页面上添加类别。但我想要添加前端的类别按钮也是如此。
示例:当我单击类别字段时,它会显示数据库中的当前类别,但我想在类别的下拉菜单中添加添加按钮。
模型.py
class PostForm(forms.ModelForm):
category = forms.ModelChoiceField(queryset=category.objects.all().order_by('name'))
class Meta:
model = Post
fields = ('title', 'category','author', 'content', 'image','status')
模板
{% if user.is_authenticated %}
<h1>Add Post...</h1>
<br/><br/>
<div class="form-group">
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.media }}
{{ form|crispy}}
<br>
<button class="btn btn-secondary btn-lg">Post</button>
</form>
【问题讨论】:
标签: python-3.x django django-views django-forms django-templates