【问题标题】:exception name 'main_page' is not defined未定义异常名称“main_page”
【发布时间】:2012-07-20 18:37:19
【问题描述】:

我是 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,或者我只是遗漏了一些明显的东西。我已经工作了几个小时,但没有看到问题。我希望有人可以提供帮助。

谢谢, 鲍比

【问题讨论】:

    标签: django nameerror


    【解决方案1】:

    在 urls.py 的顶部,添加:

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

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

    (r'^$', app.views.main_page),
    

    【讨论】:

    • 谢谢。不得不在这里和那里修复一些语法错误,但它终于可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 1970-01-01
    相关资源
    最近更新 更多