gpt4 book ai didi

python - Django 404 POST 请求

转载 作者:行者123 更新时间:2023-12-05 07:34:18 26 4
gpt4 key购买 nike

我试图在我的网站上通过 POST 发送表单,但是,POST URL 的第一部分似乎被截断,导致它无法匹配正确的 URL。

我试图在模板中发送的表单:

<form action= "{% url 'change_portrait' %}" method="post">
{% csrf_token %}
<h5>Edit Portrait</h5>
<input type="hidden" name="portrait_id" value="{{image.pk}}">
<button type="submit" name="button" class="btn btn-primary">Submit Changes</button>
</form>

正确呈现为:

<form action= "/AdminDashboard/EditPortraits/ChangePortrait/" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='iCt1xvIc1KZXErhbcMNepk2daHXApGih' />
<h5>Edit Portrait</h5>
<input type="hidden" name="portrait_id" value="14">
<button type="submit" name="button" class="btn btn-primary">Submit Changes</button>
</form>

操作 URL 正确,并且是我希望将表单提交到的位置。但是,当提交表单时,我收到一个带有以下消息的 404:

Using the URLconf defined in ZachWebsite.urls, Django tried these URL patterns, in this order:

^admin/
^AdminDashboard/
^ ^SundayPortraits/ [name='sunday_portraits']
^ ^Contact/ [name='contact_submission']
^ ^SubmitOrder/ [name='submit_buy_order']
^ ^BuyPrint/ [name='buy_print']
^ ^$ [name='index']
^ ^Login [name='login']
^ ^Logout [name='logout']
^ ^media\/(?P<path>.*)$

The current URL, EditPortraits/ChangePortrait/, didn't match any of these.

这是我的基本 urls.py

urlpatterns = [
url(r'^AdminDashboard/', include('AdminDashboard.urls')),
url(r'^', include('mainApp.urls'))
]

和 AdminDashboard.urls:

urlpatterns = [
url(r'^EditPortraits/ChangePortrait/', views.change_portrait, name='change_portrait'),
url(r'^EditPortraits/', views.edit_portraits, name='edit_portraits'),
url(r'^', views.dashboard, name='dashboard'),
]

出于某种我不知道的原因,提交表单会切断初始的“/AdminDashboard/”,并且仅针对剩余的 url“EditPortraits/ChangePortrait/”进行测试。这当然会导致它跳过基本 urls.py 中的第一个 url 模式(它应该去的地方),而是匹配捕获所有第二个 url,随后无法匹配 mainApp.urls 中的所有 url。我一辈子都弄不明白为什么提交表单会截断操作 URL 的第一部分。它在内置的 django 开发服务器上按预期工作,但在部署到生产服务器时失败。

任何帮助将不胜感激。

编辑

mainApp.urls(这些是跟踪中显示的内容,但首先不应尝试):

urlpatterns = [
url(r'^SundayPortraits/', views.sunday_portraits, name='sunday_portraits'),
url(r'^Contact/', views.contact, name='contact_submission'),
url(r'^SubmitOrder/', views.submit_buy_order, name='submit_buy_order'),
url(r'^BuyPrint/', views.buy_print, name='buy_print'),
url(r'^$', views.index, name='index'),
url(r'^Login', views.login, name='login'),
url(r'^Logout', views.logout, name='logout')
]

views.change_portrait:

def change_portrait(request):
return HttpResponse('in')

这从不显示,这意味着它永远不会到达 View 。

编辑 2:

还应注意,在提交表单时,我浏览器中的 url 是正确的:即它显示“mysite.com/AdminDashboard/EditPortraits/ChangePortrait/”。 Django 出于某种原因说当前 URL 是“/EditPortraits/ChangePortrait/”。如果我在浏览器中定期访问该网址('mysite.com/AdminDashboard/EditPortraits/ChangePortrait/'),它会正确加载页面。只有当我尝试 POST 到该页面时才会失败。

编辑 3:

可以更新 View 以正确接受 POST 变量,如下所示:

def change_portrait(request):
portrait_id = request.POST.get('portrait_id', None)
return HttpResponse('in')

但是错误仍然存​​在。

编辑 4:

呈现模板的 View :

def edit_portraits(request):
if not request.session.get('logged_in', None):
return redirect('../')

portraits = Portrait.objects.filter(isSundayPortrait=True)
context = {'portraits': portraits,
'SundayPortraits': True}
return render(request, 'AdminDashboard/editportraits.html', context)

将 url(r'^EditPortraits/ChangePortrait/', views.change_portrait, name='change_portrait') 移动到 mainApp.urls(尝试匹配的地方)会导致更新的 mainApp.urls:

urlpatterns = [
url(r'^SundayPortraits/', views.sunday_portraits, name='sunday_portraits'),
url(r'^Contact/', views.contact, name='contact_submission'),
url(r'^SubmitOrder/', views.submit_buy_order, name='submit_buy_order'),
url(r'^BuyPrint/', views.buy_print, name='buy_print'),
url(r'^$', views.index, name='index'),
url(r'^EditPortraits/ChangePortrait/$', views.change_portrait, name='change_portrait'),
url(r'^Login', views.login, name='login'),
url(r'^Logout', views.logout, name='logout')
]

mainApp.views.change_portrait:

def change_portrait(request):
if request.method == 'POST':
return HttpResponse('POST')
else:
return HttpResponse('GETTING')

使用与最初所示相同的表单,执行获取请求会按预期产生 HttpResponse“获取”。将表单更改为 POST 请求会产生新的堆栈跟踪:

Request Method: POST
Request URL: http://www.example.com/EditPortraits/ChangePortrait/
Raised by: mainApp.views.change_portrait
Using the URLconf defined in ZachWebsite.urls, Django tried these URL patterns, in this order:

^admin/
^AdminDashboard/
^ ^SundayPortraits/ [name='sunday_portraits']
^ ^Contact/ [name='contact_submission']
^ ^SubmitOrder/ [name='submit_buy_order']
^ ^BuyPrint/ [name='buy_print']
^ ^$ [name='index']
^ ^EditPortraits/ChangePortrait/ [name='change_portrait']
^ ^Login [name='login']
^ ^Logout [name='logout']
^ ^media\/(?P<path>.*)$
The current URL, ChangePortrait/, didn't match any of these.

移动到 mainApp.urls(它应该捕获它的地方)现在切断了该 url 'EditPortrait/' 的第一部分,现在只针对 'ChangePortrait/' 进行测试

最佳答案

在您的 AdminDashboard.urls 中:您所有的正则表达式都错过了 $

Note: your base urls.py doesn't need to have the $ at the end

url(r'^EditPortraits/ChangePortrait/', 
views.change_portrait, name='change_portrait')

好的是

url(r'^EditPortraits/ChangePortrait/$',
views.change_portrait, name='change_portrait')

关于python - Django 404 POST 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50223662/

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