django:如何使用materializecss在forms.py中添加选择字段
我有一个forms.py我希望能够在HTML中看到select标记我遇到了一个错误 forms.pydjango:如何使用materializecss在forms.py中添加选择字段,django,django-models,django-forms,materialize,Django,Django Models,Django Forms,Materialize,我有一个forms.py我希望能够在HTML中看到select标记我遇到了一个错误 forms.py class ArticleForm(ModelForm): class Meta: STATUS_CHOICES = ( ('d', 'Draft'), ('p', 'Published'), ) model = Article
class ArticleForm(ModelForm):
class Meta:
STATUS_CHOICES = (
('d', 'Draft'),
('p', 'Published'),
)
model = Article
fields = (
'title', 'description',
'article_header_image', 'status',
)
models.py
class Article(TimeStampedModel):
"""
Article model.
"""
STATUS_CHOICES = (
('d', 'Draft'),
('p', 'Published'),
)
title = models.CharField('title', max_length=150)
description = models.TextField('content')
status = models.CharField('article status', max_length=1,
choices=STATUS_CHOICES, blank=True, null=True, default='p')
article_header_image = models.ImageField(
upload_to="artice__header_image/%Y/%m/%d"
)
create.html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input class="btn" type="submit" value="Create" />
</form>
我没有在HTML上看到select标记
html源
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Django Class Based Views Rock</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/materialize/0.98.2/css/materialize.min.css">
<style>
.title {
text-align: center;
font-weight: 200;
}
</style>
</head>
<body>
<div class="container">
<h3 class="title">Create New Article</h3>
<form method="post" enctype="multipart/form-data">
<input type='hidden' name='csrfmiddlewaretoken' value='V6IJWF7lceEbe8pT4yN7GaCqgHnbZsbgpFWTcH9dwxzpNZzX2GFKILzpMyvs5Fls' />
<p><label for="id_title">Title:</label> <input id="id_title" maxlength="150" name="title" type="text" required /></p>
<p><label for="id_description">Content:</label> <textarea cols="40" id="id_description" name="description" rows="10" required>
</textarea></p>
<p><label for="id_article_header_image">Article header image:</label> <input id="id_article_header_image" name="article_header_image" type="file" required /></p>
<p><label for="id_status">Article status:</label> <select id="id_status" name="status">
<option value="">---------</option>
<option value="d">Draft</option>
<option value="p" selected="selected">Published</option>
</select></p>
<input class="btn" type="submit" value="Create" />
</form>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.6/js/materialize.min.js" />
</body>
</html>
如果要显示Materializecss select,则需要使用.input field类对其进行包装 你有两个选择 使用{{form.status}wrap with.input field div class使用单个表单标记
<div class="input-field">
{{ form.status }}
</div>
或使用
只需在字段中添加状态,您应该会在HTMLW中看到选择字段。您的错误是什么?我没有看到选择表单,但我在Source上有它。是的,我知道您在哪里。问题是,您正在使用materialize,选择将与那里的工作方式不同……好的,我将更改标题使用django materializecss选择表单不工作。我会试试个人的