首页 > 解决方案 > Django 教程第 1 部分:导致路径不正确 (404)

问题描述

我已经设置了 django 教程的第 1 部分,并且在尝试访问投票应用程序时收到以下“找不到页面 (404)”错误:

错误信息

Page not found (404)
  Request Method:   GET
  Request URL:  http://proto.slc.venturedata.com/polls/
  Raised by:    polls.views.index

Using the URLconf defined in proto.urls, Django tried these URL patterns, in this order:
  1. polls/
  2. admin/

The empty path didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我正在使用 Django 2.2、Python 3.7、Apache 2.4.6、mod-wsgi 4.6.5。

我能够使用内置的 Web 服务器完成本教程,但是当我将 Apache 和 mod-wsgi 添加到组合中时,我开始遇到问题。这是我的代码:

原型/urls.py:

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

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

民意调查/urls.py

from django.urls import path
from . import views

urlpatterns = [
  path('', views.index, name='index'),
]    

民意调查/urls.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the polls index.")

settings.py 中唯一改变的是 TIMEZONE。其他一切都设置为默认值。

url 和路径匹配似乎有问题。我使用的 url (proto.slc.venturedata.com/polls/) 应该与 'polls/' 路径匹配,但错误消息显示路径为空。

在实验中,我发现如果我通过将“polls/”路径替换为空路径来修改 proto/urls.py,我没有收到错误:“”。

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

我错过了什么吗?


包括非默认的 Apache 配置,因为在我将 Django 配置为使用 Apache 作为网络服务器之后问题就开始了。

ServerName 10.0.10.249:80
<Directory />
  AllowOverride none
  Require all denied
</Directory>
DocumentRoot "/var/www/html"

WSGIPythonPath /var/local/www/proto/

<VirtualHost *:80>
    DocumentRoot "/var/local/www/proto"
    ServerName proto.slc.venturedata.com

    <Directory "/var/local/www/proto">
            Require all granted
    </Directory>

    WSGIScriptAlias /polls /var/local/www/proto/proto/wsgi.py
</VirtualHost>

标签: djangoapachemod-wsgi

解决方案


Daniel Roseman 建议我包含我的 Apache 配置。当我在这篇文章中添加配置参数时,我看到了虚拟主机配置中的错误。WSGIScriptAlias 是罪魁祸首。

当前的:

WSGIScriptAlias /polls /var/local/www/proto/proto/wsgi.py

应该:

WSGIScriptAlias / /var/local/www/proto/proto/wsgi.py

进行配置更改后,教程按预期工作。

有时它只需要有人看着你的肩膀。


推荐阅读