Django使用名称中定义的URLconf,按照这个顺序尝试了这些URL模式



我有一个django项目,在这个项目中我有两个应用程序:main和profiles。

因此,我将两个迷你应用程序都添加到settings.py文件中:

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

我可以运行主应用程序-如果我去:

http://127.0.0.1:8000/

它正在运行。

但如果我去:

http://127.0.0.1:8000/profiles

然后我得到这个错误:


Page not found (404)
Request Method:     GET
Request URL:    http://127.0.0.1:8000/profiles
Using the URLconf defined in schoolfruitnvwa.urls, Django tried these URL patterns, in this order:
admin/
[name='index']
The current path, profiles, didn’t match any of these.

所以我的问题是:如何解决这个问题?

谢谢

我的view.py文件如下所示:

from django.shortcuts import render
from django.views import View
# Create your views here.

class CreateProfileView(View):
def get(self, request):
return render(request, "profiles/create_profile.html")
def post(self, request):
pass

这是我的urls.py文件:

from django.urls import path
from . import views
urlpatterns = [
path("", views.CreateProfileView.as_view())
]

很可能您需要在主应用程序的urls.py文件中添加一个条目:

from django.urls import include, path
urlpatterns = [
# ...
path('profiles/', include('profiles.urls')),
# ...
]

这样,您的个人资料应用程序的urls.py将包含在内。如果在该文件中定义条目path('', views.myview),则整个urlprofiles/将链接到该视图。

教程中的详细信息,我强烈建议您从头至尾学习:https://docs.djangoproject.com/en/4.1/intro/tutorial01/#write-您的第一次浏览

最新更新