名称错误Django

时间:2012-11-28 00:31:20

标签: django

尝试运行项目时出现名称错误,

urls.py

import os
from django.conf.urls.defaults import patterns, include, url
#from bookmarks.views import *
from django.views.generic.simple import direct_to_template
from preview.views import *
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',

    (r'^catalog/$', home),
)

views.py

# Create your views here.
from django.shortcuts import render_to_response

def home(request):
    return render_to_response("index.html")

的index.html

{% extends base.html %}
{% block content %}
    <h2>Welcome!</h2>
{% endblock %}

settings.py

TEMPLATE_DIRS = (
    "C:/python27/Djangoprojects/ecomstore/preview",
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

错误

NameError at /catalog/
name 'home' is not defined
Request Method: GET
Request URL:    http://127.0.0.1:8000/catalog/
Django Version: 1.3.1
Exception Type: NameError
Exception Value:    
name 'home' is not defined
Exception Location: C:\Python27\Djangoprojects\ecomstore\..\ecomstore\urls.py in <module>, line 12
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.2

2 个答案:

答案 0 :(得分:1)

home文件中有preview/views.py吗?不知何故,from preview.views import *没有将其导入。

在哪种情况下,你可以尝试吗

urlpatterns = patterns('preview.views',

    (r'^catalog/$', 'home'),
)

代替?

并仔细检查preview文件中INSTALLED_APPS元组中是否包含settings.py个应用。

答案 1 :(得分:0)

这是一个非常直接的错误。 “家”没有定义。

您尚未导入它。

想想打开python终端并输入

时会发生什么
> foobar
> # NameError

将其导入urls.py或使用字符串表示法'myapp.views.myfunc'