0

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

网址.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),
)

视图.py

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

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

索引.html

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

设置.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
4

2 回答 2

1

home在你的preview/views.py档案里吗?不知何故,您from preview.views import *没有将其导入。

在这种情况下,你可以尝试做

urlpatterns = patterns('preview.views',

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

反而?

并仔细检查该preview应用程序是否包含在文件的INSTALLED_APPS元组中settings.py

于 2012-11-28T00:44:34.617 回答
0

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

你还没有导入它。

想想当你打开一个 python 终端并输入时会发生什么

> foobar
> # NameError

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

于 2012-11-28T00:43:38.297 回答