python - ERROR 404 Django 项目当前路径错误

标签 python django

我正在按照本教程创建一个基本网站:

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/

相关文章:

python - Python 中的 Gamma 函数图

Python,变量的数学运算

python - 在 Selenium (Python) 中使用 XPath 选择器 'following-sibling::text()'

python - 如何测试 Django 自定义过滤器?

django - Basic Django - View 包装器如何接收请求、关键字和位置参数?

python - 如何使用 ImageSpecField 测试 Django 模型?

python - Django 自定义字段 - 从 to_python 方法获取模型字段

python - xvfb 在 ubuntu 11.04 中运行错误

python - 如何从嵌套字典中获取键?

javascript - 无法保留来自 $http.get() 的数据 - AngularJS 1.3.13