我正在按照本教程创建一个基本网站:
https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
当我尝试将主页重定向到名为 unihub 的 django 应用程序时,提示此错误:
Using the URLconf defined in TFGsWeb.urls, Django tried these URL patterns, in this order:
admin/
unihub/
^static\/(?P<path>.*)$
The current path, catalog/, didn't match any of these.
我的文件如下所示:
/TFGsWeb/settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'unihub.apps.UnihubConfig',
]
ROOT_URLCONF = 'TFGsWeb.urls'
STATIC_URL = '/static/'
/TFGsWeb/urls.py
from django.contrib import admin
from django.urls import path
# Use include() to add paths from the unihub application
from django.urls import include
# Add URL maps to redirect the base URL to our application
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('unihub/', include('unihub.urls')),
path('', RedirectView.as_view(url='/unihub/', permanent=True)),
]
# Use static() to add url mapping to serve static files during development (only)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
/TFGsWeb/unihub/urls.py
from django.urls import path
from . import views
urlpatterns = [
]
当我在我修改的文件中没有看到任何名为catalog/的应用程序或路径的引用时,我不明白为什么会出现此catalog/。
我还想将此项目上传到我的 github,那么我应该只为 key 或合理的设置信息执行此操作吗?
with open('./secret_key.txt') as f:
SECRET_KEY = f.read().strip()
最佳答案
上述网址 (/
) 上的 unihub
应用程序可能正在调用(某些 ajax)或重定向某些 catalog
应用程序网址(具体为 目录/
)。
在您的网址中包含目录应用程序的网址和/或将此类应用程序添加到您已安装的应用程序中。
另一方面,您不需要 RedirectView
将 /
指向您的 View ,只需编写:
urlpatterns = [
path('admin/', admin.site.urls),
path('unihub/', include('unihub.urls')),
path('', include('unihub.urls')),
]
关于python - ERROR 404 Django 项目当前路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54373743/