gpt4 book ai didi

python - Django URL 文件错误

转载 作者:行者123 更新时间:2023-11-30 22:24:40 26 4
gpt4 key购买 nike

我在 Django 项目中有以下 urls.py 文件,并且收到一个错误,我认为该错误与 url 和路径相关的最新语法有关。

我在 mysite(最外层目录)中的 url.py urls 文件中的代码是:

from django.urls import path
from django.conf.urls import url, include


urlpatterns = [
path(r'^$', include('aboutme.urls')),

]

错误消息是:

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^$
The empty path didn't match any of these.

在名为“aboutme”的实际应用程序(网站文件夹)中,urls.py 文件如下所示:

from django.urls import path
from django.conf.urls import url, include
from .import views #this is the main thing we are going to be doing ....returning views!

urlpatterns = [

path(r'^$', views.index,name='index'),

]

任何人都可以阐明正确的语法或我做错了什么吗?

更新:

我还返回并尝试更新主 mysite 的 url.py 文件以包含管理命令(在之前的工作版本中)。代码和产生的错误也如下所示:

尝试过此代码:

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


urlpatterns = [
path('admin/', admin.site.urls),
path(r' ', include('aboutme.urls')),

]

错误

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
admin/
The empty path didn't match any of these.

最佳答案

删除 urls.py 文件中的 ^$

from django.urls import path
from django.conf.urls import url, include

urlpatterns = [
path(r'', include('aboutme.urls')),
]

在您的应用中urls.py:

from django.urls import path
from django.conf.urls import url, include
from .import views #this is the main thing we are going to be doing

app_name="myappname"
urlpatterns = [
path(r'', views.index,name='index'),
]

在 django 2.0 中,如果您使用 path(),则不再需要它们。

相关链接:https://code.djangoproject.com/ticket/28691

关于python - Django URL 文件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47775919/

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