Django Create View需要一个字段

时间:2019-03-16 09:59:05

标签: django django-models django-forms django-views django-file-upload

我有这个模型

class Monument(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(_('nome'), max_length=150, unique=True, db_index=True)
    address = models.CharField(_('indirizzo'), max_length=255, null=False, blank=True)
    geo_coordinates = models.CharField(_('coordinate geografiche'), max_length=20, null=False, blank=True)
    description = models.TextField(_('descrizione'), blank=False, null=False)
    website_url = models.URLField(_('URL sito web'), max_length=200, null=True, blank=True)
    audioguide = models.FileField(_('audioguida'), upload_to='audioguide/', null=False, blank=False)
    opening_time = models.TimeField(_('orario apertura'), null=True, blank=True, default=now)
    closing_time = models.TimeField(_('orario chiusura'), null=True, blank=True, default=now)

这是视图

class MonumentCreateView(generic.CreateView):
    model = models.Monument
    fields = '__all__'
    template_name = 'dashboard/monuments_form.html'

    def get_context_data(self, **kwargs):
        context = super(MonumentCreateView, self).get_context_data(**kwargs)
        context['page_title'] = 'Aggiungi monumento'
        return context

这是模板

              <form class="form-horizontal form-label-left" action="{% url 'dashboard:monuments-create' %}" method="post" novalidate>{% csrf_token %}
                    {% for field in form %}
                        <div class="col-sm-offset-2 col-sm-10">
                            <span class="text-danger small">{{ field.error }}</span>
                        </div>
                        <div class="item form-group">
                            <label class="control-label col-md-3 col-sm-3 col-xs-12" for="{{ field.name }}">{{ field.label_tag }}{% if field.field.required %}(*){% endif %}</label>
                            <div class="col-md-6 col-sm-6 col-xs-12">
                                {% render_field field class="form-control col-md-7 col-xs-12" %}
                            </div>
                        </div>
                    {% endfor %}
                    <div class="ln_solid"></div>
                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-3">
                            <a href="{% url 'dashboard:monuments-list' %}"><button type="button" class="btn btn-primary">Annulla</button></a>
                            <button id="send" type="submit" class="btn btn-success">Salva</button>
                        </div>
                    </div>
                </form>

正确返回了表单,但是当我填写所有字段并继续发送表单时,它会在音频向导字段上显示“ Field Required”错误(但我选择了文件)。

0 个答案:

没有答案