默认情况下是否需要Django模型表单,以及如何将整个表单的required设置为false?

models.py:

class Venue(models.Model):
    author = models.ForeignKey(ProfileUser, on_delete=models.CASCADE)
    title = models.CharField(max_length=300)
    city = models.ForeignKey(City, on_delete=models.CASCADE)
    address = models.CharField(max_length=300)
    phone = models.CharField(max_length=20, default='')
    email = models.CharField(max_length=100, default='')
    site = models.CharField(max_length=100, default='')
    facebook = models.CharField(max_length=100, default='')
    instagram = models.CharField(max_length=100, default='')
    content = models.TextField()
    rating = models.DecimalField(default=10.0, max_digits=5, decimal_places=2)
    created_date = models.DateTimeField(default=timezone.now)
    approved_venue = models.BooleanField(default=False)
    admin_seen = models.BooleanField(default=False)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    def __str__(self):
        return f"{self.title}"

表格

class VenueForm(forms.ModelForm):
    class Meta:
        model = Venue
        fields = [
            'title',
            'content',
            'city',
            'address',
            'phone',
            'email',
            'site',
            'facebook',
            'instagram',
        ]

    def __init__(self, *args, **kwargs):
        super(VenueForm, self).__init__(*args, **kwargs)
        self.fields['title'].label = "Име"
        self.fields['content'].label = 'Описание'
        self.fields['city'].label = 'Град'
        self.fields['address'].label = 'Адрес'
        self.fields['phone'].label = 'Телефон'
        self.fields['email'].label = 'E-mail'
        self.fields['site'].label = 'Сайт'

html渲染:

<form method="post" id="dialog_addVenue_part" enctype="multipart/form-data">
    {% csrf_token %}
    {% for hidden in postForm.hidden_fields %}
        {{ hidden }}
    {% endfor %}
    {% for field in form %}
        <div class="fieldWrapper">
            <div class="errorcode{{field.html_name}}">
                {{ field.errors }}
            </div>
            {{ field.label_tag }} {{ field }}
            {% if field.help_text %}
            <p class="help">{{ field.help_text|safe }}</p>
            {% endif %}
        </div>
    {% endfor %}
    <div id="map" style="height: 500px;">

    </div>
    <div class="utf_addVenue_form">
        <button type="submit" value="Изпрати">Изпрати</button>
    </div>
</form>

带有必填字段的HTML部分:

    <div class="fieldWrapper">
        <div class="errorcodesite">

        </div>
        <label for="id_site">Сайт:</label> <input type="text" name="site" maxlength="100" required="" id="id_site">

</div>

<div class="fieldWrapper">
    <div class="errorcodefacebook">

    </div>
    <label for="id_facebook">Facebook:</label> <input type="text" name="facebook" maxlength="100" required="" id="id_facebook">

</div>

我没有在任何地方指出所有字段都必须“填写”表格,但表格附带了这些字段。那么,默认情况下是否需要Django模型表单,以及如何将整个表单的required设置为false?

评论
墨小沫
墨小沫

Add blank=True to the fields you don't need to be required.

class Venue(models.Model):
    . . .
    title = models.CharField(max_length=300, blank=True)
    . . .
点赞
评论