【问题标题】:Using the URLconf defined in myproject2.urls, Django tried these URL patterns, in this order:使用 myproject2.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:
【发布时间】:2021-04-13 14:27:57
【问题描述】:
urls.py:
-------
from django.urls import path

from . import views
urlpatterns = [
    path('hello', views.hello, name='hello'),
]

views.py:
--------
from django.shortcuts import render
from django.http import HttpResponse

# create your view here

def hello(request):
    # return HttpResponse('<h1>Hello World</h1>')
    return render(request,'home.html',{'name':"Page"})

Screenshot1: https://i.stack.imgur.com/vfXEf.png

Screenshot2: https://i.stack.imgur.com/igqQe.png

错误显示: 使用 myproject2.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

1.admin/

【问题讨论】:

  • 你能从你的项目中添加 urls.py 吗?
  • 看你的截图你有两个urls.py。您帖子中的摘录应位于myproject2/urls.py。然而from . import views 表明它实际上在helloworld/urls.py 中。你能澄清一下吗?
  • helloworld/urls.py

标签: python django


【解决方案1】:

常见错误'/'在定义的hello url中丢失:

在您的应用程序 urls.py 中更正此错误:

path('hello/', views.hello, name='hello')

在您的项目urls.py(这是您在myproject2 文件夹中的urls.py 文件)中包含您的应用程序网址(这是您在helloworld 应用程序中的urls.py 文件),如下所示:

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

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

另外不要忘记将您的应用添加到列表变量中的settings.py,与其他应用类似:

INSTALLED_APPS = [
    'helloworld',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

在你搞砸之前,先获得最好的教程来了解它是如何工作的!

【讨论】:

    猜你喜欢
    • 2021-02-14
    • 2017-05-27
    • 2017-06-26
    • 2020-05-05
    • 1970-01-01
    • 2018-06-28
    • 2020-08-02
    • 1970-01-01
    • 2020-09-01
    相关资源
    最近更新 更多