【发布时间】:2018-10-31 08:10:22
【问题描述】:
我写了下面一段简单的代码,我只是想检查一下在访问POST方法时是否显示405,而是说找不到页面。
views.py
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .forms import helloform
def index(request):
form = helloform()
return render(request, 'hello/index.html', {'form' : form})
def addintodb(request): #trying to invoke this function
form = helloform(request.POST)
print(request.POST)
return redirect(index)
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('add', views.addintodb, name='addtodb'), #using this url
]
index.html
> form action="{% url 'addtodb' %}" method="POST" role="form" # from here
> ...
> </form>
一段时间后我发现,将我的项目 URL 设置为“”可以满足要求。 (即)
我的项目的 urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello.urls')) #previously I had path('hellobfs', include('hello.urls'))
]
所以删除我的项目 url 的任何路径都可以让我的应用程序的 url 正常工作而没有“404”错误,有人可以解释为什么吗?
【问题讨论】:
-
405 不代表成功。
-
但它实际上应该说 405,当我尝试访问 hellobfs/add 时方法不允许。那些'*'被放置用于在Stackoverflow中将行标记为粗体,
-
请正确格式化您的代码。在 Python 中它是必不可少的,否则我们可能无法理解。