gpt4 book ai didi

python - Django - 为什么编辑表单发布编辑 url + 创建 url 相互重叠?

转载 作者:太空宇宙 更新时间:2023-11-03 16:37:40 24 4
gpt4 key购买 nike

我正在将“创建”表单重新用于编辑 View 。 “创建”路由工作得很好,但是当提交“编辑”表单时,我的终端显示它尝试POST/comment/edit/26/comment/add/

我正在尝试自定义类 View ,因此切换到通用 UpdateView:

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

仍然是同样的事情。所以我想这可能是 ModelForm 或 url 的问题?

这里是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>

aa和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')
]

所以我读到,表单的操作会自动设置为呈现它的 View 。看起来它得到了(/comment/edit/26/),但每次也会在最后抛出comment/add/。不知道还能去哪里看。

最佳答案

您需要在开始时锚定您的 URL 模式。

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

关于python - Django - 为什么编辑表单发布编辑 url + 创建 url 相互重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37083251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com