Python Django[错误404页面未找到url]

2024-10-08 18:29:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我写了下面这段简单的代码,我只想检查一下当访问POST方法时,它是否显示405,但它却显示page not found。在

在视图.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)

在网址.py在

^{pr2}$

在索引.html在

> form action="{% url 'addtodb' %}" method="POST" role="form" # from here
>     ...                      
>     </form> 

经过一段时间后,我发现,将我的项目URL设置为“”可以提供所需的。也就是说

我的项目网址.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'))
]

因此,删除我的项目网址的任何路径,使我的应用程序的网址工作没有“404”错误,有人能解释为什么吗?在


Tags: path项目djangofrompyimportformhello
3条回答

return redirect(index)替换为return redirect('/', )。您的代码应该如下所示:

def addintodb(request): #trying to invoke this function
    form = helloform(request.POST)
    print(request.POST)
    return redirect('/', )  #<  this is the url pattern for your index

我想这个,作为网址.py在我的应用程序中需要项目和应用程序的URL

那就是 项目url->

path('hellobfs', include('hello.urls'))

应用程序的url->

^{pr2}$

当我需要遍历到添加页面时,我需要提供(我犯错误的地方)

127.0.0.1:8000/hellobfsadding

它提供了不允许的方法,为了提高可读性,我们可以在project的url中添加一个“/”

path('hellobfs/', include('hello.urls'))

现在我们可以穿越过去了

127.0.0.1:8000/hellobfs/adding

我想你需要把你的表格保存在视图.py文件尝试添加一个,窗体保存()在您的,form=helloform之后(请求.POST)在

相关问题 更多 >

    热门问题