Django Updateview无法保存数据和重定向

时间:2015-06-30 15:34:19

标签: django django-views

模板

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Update" name="_save" />
</form>

形式

class LabelUpdateForm(forms.ModelForm):

    def clean(self):
        return self.cleaned_data

    class Meta:
        model = models.SlPannelli
        fields = '__all__'

查看

class LabelUpdateView(UpdateView):
    model = models.SlPannelli
    exclude=()
    fields = '__all__'
#    form_class = LabelUpdateForm  

    def form_valid(self, form):
        return super(LabelUpdateView, self).form_valid(form)
        return TemplateResponse(self.request, self.template_name, locals())

网址

url(r'^labelch/(?P<pk>\d+)$', views.LabelUpdateView.as_view(), name='label_update'),

我可以看到加载的数据用于更新,但是当点击更新按钮时,不断重定向到&#34; labelch /&#34;页面,无法保存更新的数据。

Vedi Informazioni
Funzione View   Argomenti   Parole chiave   Nome URL
<nessuna view>
u'None'
u'None'
None
Cookies
Variabile   Valore
'cookieconsent_dismissed'
'yes'
'csrftoken'
'vUdhIR42Z4c3aCgbeVugQidDHoa8gFby'
'djdt'
'hide'
'sessionid'
'f9957eaw537wg6v8xi069dqq5cqyrvxd'
Dati di sessione
Variabile   Valore
u'_auth_user_backend'
u'django.contrib.auth.backends.ModelBackend'
u'_auth_user_hash'
u'55822a00e348b79b5cd03be74b185995766915a7'
u'_auth_user_id'
u'1'
Nessun dato in GET
Dati POST
Variabile   Valore
u'_save'
[u'Update']
u'csrfmiddlewaretoken'
[u'vUdhIR42Z4c3aCgbeVugQidDHoa8gFby', u'vUdhIR42Z4c3aCgbeVugQidDHoa8gFby']
u'note'
[u'']
u'tipo'
[u'S2']

这是 REQUEST django debug tolbar

的输出

1 个答案:

答案 0 :(得分:0)

[解决]

url(r'^(?P<pk>\d+)/labelch$', views.LabelUpdateView.as_view(), name='label_update'),

无法理解为什么但是在URL中更改pk的顺序解决了所有问题!

@simone感谢您的建议,将来会记住!