django urls.py 正则表达式不工作

0 投票
1 回答
545 浏览
提问于 2025-04-16 12:08

这是关于在Wamp Server上运行的Django 1.2.5和Python 2.7,使用的Apache版本是2.2.17。

我遇到的问题是,我在urls.py文件中的URL配置没有正确重定向,结果只是出现了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,不知道我的代码哪里出了问题。以下是我的Python路径:

['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']

1 个回答

4

是的,你的链接应该像这样:

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

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

所以它在寻找一个叫 index 的模块,但这个模块并不存在(如果你没有使用 include(),它会在找一个函数)。

撰写回答