下拉菜单未以django呈现的形式出现在模板中

时间:2018-09-01 13:43:06

标签: django django-forms django-templates

我试图在Django模板中显示一个下拉菜单,但没有出现。这是我的代码:

发布模型:

class Post(models.Model):
    category_choices = (
        ('technology', 'Technology'),
        ('personal', 'Personal'),
        ('poetry', 'Poetry'),
        ('rants', 'Rants'),
        ('random', 'Random'),
    )
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    title = models.CharField(max_length=70)
    body = models.TextField()
    summary = models.CharField(max_length=100)
    created_date = models.DateTimeField(default=timezone.now())
    published_date = models.DateTimeField(blank=True, null=True)
    slug = models.SlugField(max_length=40, unique=True)
    category = models.CharField(max_length=10, choices=category_choices, default='technology')

表格:

class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'category', 'body')

模板:

<h1>New post</h1>
<div class="row">
    <form method="POST" action="{% url 'post_new' %}" class="col s12 m12 l12"> {% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="btn">Save</button>
    </form>
</div>

这是它在浏览器中的外观: https://i.stack.imgur.com/uMXMb.png

1 个答案:

答案 0 :(得分:0)

您可以在表单标签中尝试{{ form.category }}吗?只是为了测试。