1

我是 Django 的新手。设置是ubuntu服务器。已创建项目、数据库,编辑了 settings.py、views.py、urls.py 文件。创建了一个模板文件夹和一个 main_page.html 文件。所有这些都来自 django:visual quickpro 书。运行开发服务器时,我不断收到名称错误。它说未定义名称“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,或者我只是遗漏了一些明显的东西。我已经工作了几个小时,但没有看到问题。我希望有人能帮帮忙。

谢谢,鲍比

4

1 回答 1

3

在 urls.py 的顶部,添加以下内容:

import app.views   # where "app" is the name of your app

然后将 urls.py 中的行更改为:

(r'^$', app.views.main_page),
于 2012-07-22T01:15:28.280 回答