Django - CreateView失败,重定向到空创建视图

时间:2018-02-05 11:50:22

标签: python django

我的CreateView似乎失败但我不确定为什么。我被重定向到一个没有错误或消息的空CreateView,告诉我它失败的原因。

我原本以为我会被重定向到CreateView,并保存输入的数据和错误。我已经检查了管理员的模型,并且没有创建记录。

问题似乎是服务器端,但我没有在控制台得到任何输出我只得到200s

"POST /sites/site/add_subnet/7 HTTP/1.1" 200 41406

my forms.py:

class AddSubnetForm(forms.ModelForm):
    class Meta:
        model = SiteSubnets
        fields = ['device_data','subnet', 'subnet_type', 'circuit', 'vlan_id', 'peer_desc']

    def __init__(self, *args, **kwargs):
        site_id = kwargs.pop('site_id', None)
        super(AddSubnetForm, self).__init__(*args)
        self.fields['circuit'].queryset = Circuits.objects.filter(site_data=site_id)

view.py:

class AddSubnet(CreateView):
    form_class = AddSubnetForm
    template_name = "sites/edit_subnet.html"

@method_decorator(user_passes_test(lambda u: u.has_perm('config.add_subnet')))
def dispatch(self, *args, **kwargs):
    self.site_id = self.kwargs['site_id']
    self.site = get_object_or_404(SiteData, pk=self.site_id) 
    return super(AddSubnet, self).dispatch(*args, **kwargs)

def get_success_url(self, **kwargs):         
        return reverse_lazy("sites:site_detail_subnets", args=(self.site_id,))

def form_valid(self, form):
    form.instance.site_data = self.site
    return super(AddSubnet, self).form_valid(form)

def get_form_kwargs(self, *args, **kwargs):
    kwargs['site_id'] = self.site_id
    return kwargs

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['SiteID']=self.site_id
    context['SiteName']=self.site.location
    context['FormType']='Add'
    context['active_subnets']='class="active"'

    return context

模板:

{% block content %}
<div class="col-lg-3">
    {{ result }}
    {{ form.errors }}
    {% if messages %}
        {% for message in messages %}
            alert(message);
        {% endfor %}
    {% endif %}
    <form method="post">
        {% csrf_token %}

        {% for hidden_field in form.hidden_fields %}
            {{ hidden_field }}
        {% endfor %}

        {% for field in form.visible_fields %}
        <div class="form-group">
            {{ field.label_tag }}

            {% if form.is_bound %}
            {% if field.errors %}
                {% render_field field class="form-control is-invalid" %}
                {% for error in field.errors %}
                <div class="invalid-feedback">
                    {{ error }}
                </div>
                {% endfor %}
            {% else %}
                {% render_field field class="form-control is-valid" %}
            {% endif %}
            {% else %}
            {% render_field field class="form-control" %}
            {% endif %}

            {% if field.help_text %}
            <small class="form-text text-muted">{{ field.help_text }}</small>
            {% endif %}
        </div>
        {% endfor %}
        <div class="form-group">
            <input type="submit" value="{{ FormType }} Subnet" class="btn btn-primary" />
            {% if FormType != 'Add' %}
                <a class="btn btn-danger" href="{% url 'sites:site_overview' SiteID %}">Delete</a>
            {% endif %}
        </div>      
    </form>
</div>
<!-- /.col-lg-3 -->
{% endblock %}

1 个答案:

答案 0 :(得分:1)

我认为问题在于您未在super方法中调用get_form_kwargs,因此POSTed数据实际上并未传递给表单。你可能需要:

def get_form_kwargs(self):
    kwargs = super().get_form_kwargs()
    kwargs['site_id'] = self.site_id
    return kwargs