django中的urls.py抛出“找不到模块”

时间:2013-06-29 17:35:59

标签: python django django-urls

我对Django中的urls.py有疑问。我正在建立一个博客,从头开始学习Django。在主urls.py文件中,我已经指定了我的应用程序的urls.py的包含路径。

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

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

urlpatterns = patterns('',

    (r'^', include('myblog.urls')),
)

在app(名为myblog)中,urls.py的内容如下:

from django.conf.urls.defaults import *
from models import blogmodel
from django.contrib import admin

urlpatterns = patterns('',
    (r'^login/', include(admin.site.urls)),
    (r'^$', include('myblog.views.getLatest')),

)

其中getLAtest是我的views.py中的函数。错误显示 没有名为getLatest的模块

这是我的views.py,

from django.shortcuts import render_to_response
from myblog.models import blogdb

def getLatest(request):
    post = blogdb.objects.all()
    sorted_post = post.order_by('-served_date')
    return render_to_response('blogs.html', {'posts':sorted_post})

感谢任何帮助。提前谢谢..

2 个答案:

答案 0 :(得分:2)

你使用了错误的指令; include()用于包含另一个包;当您使用该指令时,Django将在包urls.py中查找myblog.views.getLatest

您想要改为命名视图:

urlpatterns = patterns('',
    (r'^login/', include(admin.site.urls)),
    (r'^$', 'myblog.views.getLatest'),
)

注意:没有使用include()

答案 1 :(得分:1)

尝试更新此内容:

urlpatterns = patterns('',
    (r'^login/', include(admin.site.urls)),
    (r'^$', include('myblog.views.getLatest')),
)

到此:

urlpatterns = patterns('',
    (r'^getLatest/$', 'myblog.views.getLatest'),
)

include用于读取另一个urls.py文件,您希望在该文件中执行特定的视图功能。