2016-05-07 18 views
1

我正在重新使用編輯視圖的「創建」表單。 '創建'路由工作得很好,但是當提交'編輯'表單時,我的終端說它試圖登錄POST /comment/edit/26/comment/add/Django - 爲什麼編輯表單發佈來編輯url +在彼此之上創建url?

我試圖自定義類視圖,因此再切換到剛纔通用的UpdateView:

class CommentUpdate(UpdateView): 
    model = Comment 
    fields = ['title'] 

,仍然同樣的事情。所以我想這可能是ModelForm或者urls的問題?

這裏的forms.py

from django import forms 
from comments.models import Comment 


class CommentForm(forms.ModelForm): 
class Meta: 
    model = Comment 

等 - 的形式本身看起來像這樣:

<form id="new-form" role="form" method="post"> 
     {% csrf_token %} 
     <p>{{ form.title }}</p> 
     <p> 
     <button type="submit">Submit</button> 
     <a href="/">Cancel</a> 
     </p> 
</form> 

AAAND urls.py:

from django.conf.urls import url 
from django.contrib import admin 

from ratings.views import (home, CommentCreate, CommentUpdate) 

urlpatterns = [ 
    url(r'^$', home, name='comment-home'), 
    url(r'comment/add/$', CommentCreate.as_view(), name='comment-add'), 
    url(r'comment/edit/(?P<pk>\d+)/$', CommentUpdate.as_view(), name='comment-edit') 
] 

所以我讀過表單將其操作自動設置爲呈現它的視圖。看起來像是(/comment/edit/26/),但最後每次都會調用comment/add/。不知道在哪裏看。

回答

1

您需要在開始時錨定您的網址格式。

url(r'^comment/add/$', CommentCreate.as_view(), name='comment-add'), 
url(r'^comment/edit/(?P<pk>\d+)/$', CommentUpdate.as_view(), name='comment-edit') 
+0

完全就是這樣。還發現我有一個AJAX處理表單ID的流浪js處理器。這很糟糕 - 但是,肯定需要將url定位到url。 – DangerAle

0

嘗試添加以下到您的CommentCreateCommentUpdate觀點:

form_class = CommentForm 

<form>缺少action屬性可以干擾爲好。這可能有助於添加action=""