Django formset:由于错误而无法提交formset

时间:2017-10-31 17:46:29

标签: django django-forms

以下是我的模特:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

sf = s.makefile("rw")

for i in file.readlines():

    password = i.strip('\n')

    credentials='root'+","+password

    sf.write("{}\n".format(credentials))

形式:

class ContentLineConfig(models.Model):
    content_template = models.ForeignKey(ContentTemplate, null=False, verbose_name="Content template")
    line_x_start_cord = models.IntegerField(null=False, verbose_name="Line start X cordinate")
    line_y_start_cord = models.IntegerField(null=False, verbose_name="Line start Y cordinate")
    line_x_end_cord = models.IntegerField(null=False, verbose_name="Line end  X cordinate")
    line_y_end_cord = models.IntegerField(null=False, verbose_name="Line end Y cordinate")
    line_fill_color = models.CharField(max_length=10, default="WHITE", choices=COLOR_CHOICE,
                                   verbose_name="Line fill color")
    line_width = models.IntegerField(null=False, verbose_name="Line width")
    archive = models.BooleanField(default=False)

查看:

class ContentLineConfigForm(ModelForm):
    class Meta:
        model = ContentLineConfig
        fields = '__all__'
        exclude = ['id','archive','content_template',]

模板:

class ContentLineConfigView(ModelFormSetView):
    model = ContentLineConfig
    form_class = ContentLineConfigForm
    success_url = reverse_lazy('content_templates_list')
    template_name = 'formtemplates/test_formset.html'

    def form_valid(self,form):
        instance = form.save(commit=False)
        instance.content_template_id = self.kwargs.get('pk')
        return super(ContentLineConfigView, self).form_valid(form)

    def form_invalid(self,form):
        return super(ContentLineConfigView, self).form_invalid(form)

URL:

<div class="container">


    <form action="." method="post">
        {% csrf_token %}

        {{ formset.management_form }}
        {% for form in formset %}
        {{ form.as_p }}
        {% endfor %}
        <input type="submit" value="Submit" />
    </form>

</div>

当我提交formset时,我收到以下错误:

url(r'^ct/(?P<pk>[\w-]+)/lines/create/$', 
ContentLineConfigView.as_view(), name='content_line_create')

但是当我详细看到错误页面时,我看到以下内容:

NOT NULL constraint failed: 
contenttemplates_contentlineconfig.content_template_id

为什么会出现这个错误?

另外,如果我提交空的formset,我会被重定向到success_url,根本没有验证,为什么要跳过验证?

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

pk

想知道url(r'^ct/(?P<pk>\d+)/lines/create/$',怎么可能是char而不是数字!

{{1}}