【问题标题】:django urls.py regex isn't workingdjango urls.py 正则表达式不起作用
【发布时间】:2011-02-19 23:16:49
【问题描述】:

这适用于运行 apache 版本 2.2.17 的 Wamp Server 上的 Django 1.2.5 和 Python 2.7。

我的问题是 urls.py 中的我的 URLConf 没有重定向,它只是抛出 404 错误。

urls.py:

from django.conf.urls.defaults import *

# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()

urlpatterns = patterns('',
    (r'^app/$', include('app.views.index')),

    # Uncomment the admin/doc line below to enable admin documentation:
    #(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    #(r'^admin/', include(admin.site.urls)),
)

views.py

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello World")

我收到以下错误: /app/ 处的 ImportError 没有名为 index 的模块

我只是在学习 Django,所以我很难过,有人能看出我的代码有什么问题吗?这是我的 PythonPath: ['C:\Windows\system32\python27.zip', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\wamp \bin\apache\Apache2.2.17', 'C:\wamp\bin\apache\apache2.2.17\bin', 'C:\Python27', 'C:\Python27\lib\site-packages', 'c: \wamp\www\seetwo']

【问题讨论】:

    标签: python django


    【解决方案1】:

    是的,您的网址应如下所示:

    (r'^app/$', 'app.views.index'),
    

    使用include 语句意味着您指向一个新的urlconf 文件! http://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs

    所以它正在寻找一个不存在的模块index(而不是一个函数,如果你没有使用include()

    【讨论】:

    • 轰隆隆,来了!现在已修复,谢谢 Yuji,如果允许,我会在大约 7 分钟内接受您的答复。 :)
    猜你喜欢
    • 2014-06-14
    • 2013-07-12
    • 2012-07-22
    • 2020-08-31
    • 2014-09-02
    • 2016-07-21
    • 1970-01-01
    • 2013-09-23
    • 1970-01-01
    相关资源
    最近更新 更多