python - Django - 页面未找到 404

标签 python django

我正在尝试使用 Django 创建一个网站。我的索引 View 正在工作,但是我想创建一个简单的自定义 View 并映射到它,但我无法做到。我收到 404 错误。

我的项目中的应用程序称为电子邮件。

以下是文件:

base/urls.py

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^emails/$', include('emails.urls')),
)

电子邮件/urls.py

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

from emails import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^custom/$', views.custom, name='custom'),
)

电子邮件/views.py

from django.http import HttpResponse

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

def custom(request):
    return HttpResponse("This is a custom view.")

这是404:

使用 crm.urls 中定义的 URLconf,Django 按以下顺序尝试了这些 URL 模式:

^admin/
^/emails/$

当前 URL、电子邮件/自定义与其中任何一个都不匹配。

当我访问localhost:8000/emails/时 - 我看到索引 View 。但是 localhost:8000/emails/custom/ 返回 404 错误。任何帮助将不胜感激!

最佳答案

url(r'^emails/$', include('emails.urls')), 不应包含美元符号 $:

url(r'^emails/', include('emails.urls')),

您不想在 emails 之后结束路径,因此不要以正则表达式字符 $ 结束字符串。让它能够在其他urls.py文件中继续

关于python - Django - 页面未找到 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24445635/

相关文章:

python - MovieWriter ffmpeg 不可用;尝试使用 <class 'matplotlib.animation.PillowWriter' > 代替

python - 在 plt.show() 之后指定 Matplotlib 中缩放图像的高度和宽度

python - Odoo 不显示 xpath 中的字段标签

python - 尝试使用客户错误处理程序时出现 'KeyError: None' 错误 - Flask

Python:如何使一个变量在 40% 的时间内为真

python - 在 Django 中以不同模型的形式显示多对多?

django - 模板标签中使用的循环变量?

python - django-使文件字段可选时出错

python - 我可以使用 python-django 框架开发 Web 应用程序并将其作为桌面应用程序运行吗?

python - 使用 CloudSQL : How to connect database (Error 2002, 的 Google AppEngine 上的 Django 无法连接到本地 MySQL 服务器..)