【问题标题】:django: how to add a select field in the forms.py with materializecssdjango:如何使用 materializecss 在 forms.py 中添加选择字段
【发布时间】:2017-05-25 06:13:26
【问题描述】:

我有一个 forms.py 我希望能够在 HTML 中看到 select 标记我收到一个错误

forms.py

    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"
            )

创建.html

  <form method="post" enctype="multipart/form-data">
      {% csrf_token %}
      {{ form.as_p }}
      <input class="btn" type="submit" value="Create" />
  </form>

我在 HTML 上看不到选择标签

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>

【问题讨论】:

  • 只需在fields 中添加status,您应该会在HTML 中看到选择字段
  • 你的错误是什么?
  • 我没有看到选择表单,但我确实在源文件中有它
  • 是的,我知道你在哪里。问题是您正在使用物化,并且选择的工作方式与那里不同......
  • 好的,我改一下标题

标签: django django-models django-forms materialize


【解决方案1】:

如果你想显示 Materializecss 选择然后你想用 .input-field 类 @987654321@ 包装它

您有两种选择。

  1. 使用带有 .input-field div 类的 {{ form.status }} 包装使用单个表单标签

    <div class="input-field">
      {{ form.status }}
    </div>
    
  2. 或使用@987654322@

【讨论】:

  • 使用 django-materializecss-form 的选择不起作用。我会尝试个人
猜你喜欢
  • 1970-01-01
  • 2016-09-11
  • 2022-06-25
  • 2011-09-20
  • 2018-03-05
  • 1970-01-01
  • 2021-09-13
  • 2011-07-02
  • 2021-04-25
相关资源
最近更新 更多