在django中找不到页面,pycharm吗?

问题描述 投票:-7回答:1

我正在与django项目一起创建一个网站当我设置第一个URL时,它可以正常工作,但是当我在products / new /之后添加新的URL时,则显示找不到页面

找不到页面(404)请求方法:GET要求网址:http://127.0.0.1:8000/products/new/Django使用pyshop.urls中定义的URLconf,按以下顺序尝试了这些URL模式:

这里是文件夹,

products.urls.py:

from django.urls import path
from . import views


urlpatterns = [
path('', views.index),
path('new', views.new)
]

views.py:

from django.http import HttpResponse
from django.shortcuts import render


def index(request):
return HttpResponse('Hello world')


def new(request):
return HttpResponse('New Products!!!!!!!!!!!!!')

apps.py:

from django.apps import AppConfig


class ProductsConfig(AppConfig):
name = 'products'

pyshop.urls.py:

"""pyshop URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import:  from my_app import views
2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
1. Add an import:  from other_app.views import Home
2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('products/', include('products.urls'))
]

这是错误

找不到页面(404)请求方法:GET请求URL:http://127.0.0.1:8000/products/new/使用在pyshop.urls,Django按以下顺序尝试了这些URL模式:

管理员/产品/产品/新产品

当前路径,products / new /,与任何这些都不匹配。

python django django-urls
1个回答
0
投票

对于您的应用程序,使用单数名称而不是复数:

产品代替产品。


模板目录

首先在产品应用中创建模板目录,然后在模板中创建另一个目录,文件夹名称必须是您的应用名称(产品)。

products # app name 
   templates # templates folder
       products # again app name
          aboutme.html # here your all html pages
          mypage2.html

Views.py

from django.http import HttpResponse
from django.shortcuts import render


def index(request):
    return HttpResponse('Hello world')

def new(request):
    return HttpResponse('New Products!!!!!!!!!!!!!')

#or if you want to return html page then 
def aboutme(request):
   return render(request,'products/aboutme.html')

产品应用urls.py

from django.urls import path
from . import views


urlpatterns = [
path('', views.index),
path('new', views.new),
path('all-about-me/', views.aboutme,name = 'about_me'), # get rid of hard coded urls so always use name parameter to give some name to your view.  
] 

Project urls.py文件

在此处添加所有应用程序的URL。

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
     path('admin/', admin.site.urls),
     path('products/', include('products.urls'))
]

[将您的应用添加到设置中

INSTALLED_APPS = [
    # others goes here

    'products.apps.ProductsConfig',# your products app

]

您的项目目录以及代码必须与上面相同。

您缺少我上面描述的任何上述步骤,并且在URLS中造成混淆。

[http://127.0.0.1:8000它显示PageNotFound 404,因为您没有在主URLS.py中定义任何索引页

[http://127.0.0.1:8000/products它显示'Hello world'(称为索引视图)。

http://127.0.0.1:8000/products/new它显示为“新产品!!!!!!!!!!” (称为新视图)。

http://127.0.0.1:8000/products/all-about-me它呈现aboutme.html(称为aboutme view)

仍然感到困惑,发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.