我是django的新手。 setup是ubuntu服务器。创建了一个项目,数据库,编辑了settings.py,views.py,urls.py文件。创建了模板文件夹和main_page.html文件。所有这些来自django:visual quickpro book。运行开发服务器时,我不断收到名称错误。它说没有定义名称'main_page'。
这是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'^$', main_page),
# Examples:
# url(r'^$', 'chapter2.views.home', name='home'),
# url(r'^chapter2/', include('chapter2.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
# url(r'^admin/', include(admin.site.urls)),
)
这是views.py;
# Create your views here
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
def main_page(request):
template = get_template('main_page.html')
variables = Context({'head_title': 'First Application',
'page_title': "Welcome to our first application',
'page_body': "This is our first application, pretty good, eh?'})
output = template.render(variables)
return HttpResponse(output)
我认为书中存在语法错误,它可以满足Windows的需求,或者我只是遗漏了一些明显的东西。我已经工作了几个小时,但没有看到问题。我希望有人可以提供帮助。
谢谢, 波比
答案 0 :(得分:3)
在你的urls.py顶部,添加:
import app.views # where "app" is the name of your app
然后将urls.py中的行更改为:
(r'^$', app.views.main_page),