gpt4 book ai didi

python - Django Form提交导致404错误

转载 作者:行者123 更新时间:2023-12-01 01:15:24 27 4
gpt4 key购买 nike

当用户提交表单时,我收到以下错误:

Page not found (404)
Request Method: csrfmiddlewaretoken=uaL0Ogej2bd0oSaNLXYwu1CxSPWz6mcs0PuXiwM2mpe01VecK5IVBK40xvqcFCJF&views=0&likes=0&slug=&name=do+do+ahadalfjkdas%3Bldfjksal%3B12321&submit=Create+CategoryPOST
Request URL: http://127.0.0.1:8000/rango/add_category/rango/add_category/
Using the URLconf defined in tango_with_django_project.urls, Django tried these URL patterns, in this order:

^$ [name='index']
^admin/
^rango/ ^$ [name='index']
^rango/ ^about/ [name='about']
^rango/ ^category/(?P<category_name_slug>[\w\-]+)/$ [name='show_category']
^rango/ ^page/(?P<page_name_slug>[\w\-]+)/$ [name='show_page']
^rango/ ^add_category/$ [name='add_category']
The current path, rango/add_category/rango/add_category/, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

似乎我附加了 rango/add_category 两次,并且没有重新引用回索引页。但我不确定我忽略了什么。

这是相关模板:

<!-- created in c7 for form viewing-->

<!DOCTYPE html>
<html>
<head>
<title>Rango</title>
</head>

<body>
<h1>Add a Category</h1>
<div>
<form id="category_form" method="post" action="rango/add_category/">
{% csrf_token %}
{% for hidden in form.hidden_fields %}
{{hidden}}
{% endfor %}
{% for field in form.visible_fields %}
{{ field.errors }}
{{ field.help_text }}
{{ field }}
{% endfor %}
<input type="submit" name="submit" value="Create Category" />
</form>
</div>
</body>
</html>

以及相关的表单文件:

#file added c7, forms 
from django import forms
from rango.models import Page, Category

class CategoryForm(forms.ModelForm):
name = forms.CharField(max_length=128,
help_text="Please enter the category name.")
views = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
likes = forms.IntegerField(widget=forms.HiddenInput(), initial=0)
slug = forms.CharField(widget=forms.HiddenInput(), required=False)

#Inline class to provide additional info on the form
class Meta:
#provide an association b/t ModelForm and model
model = Category
fields = ('name',)

相关的url文件:

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^about/', views.about, name='about'),
#?P makes group to match the slug
url(r'^category/(?P<category_name_slug>[\w\-]+)/$',
views.show_category, name='show_category'),
#page slug added at ex at end of 6
# not sure if needed, given index view ...
url(r'^page/(?P<page_name_slug>[\w\-]+)/$',
views.show_page, name='show_page'),
#show page added in ex at end of 6
#next added at c7 for forms
#ordering may matter for processing of requests -- see official docs
url(r'^add_category/$', views.add_category, name='add_category')]

View 中的相关 View :

def add_category(request):
form = CategoryForm()

#HTTP Post? (that is, did user supply data?)
if request.method == 'POST':
form = CategoryForm(request.POST)

if form.is_valid():
form.save(commit=True)
# could also give confirmation message if you wanted
return index(request)

else:
print(form.errors)

return render(request, 'rango/add_category.html', {'form': form})
# new template created

提前致谢。

最佳答案

尝试更改表单的操作网址

<form id="category_form" method="post" action="{% url 'add_category' %}">

更多信息请阅读https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#url

关于python - Django Form提交导致404错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54393951/

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