Django'全球名称'错误

时间:2016-02-14 17:49:21

标签: python django

我正在设置我的第一个Django应用程序。我在使数据库模型工作时遇到了一些麻烦。

这是我的模板:

{% extends 'layouts/base.html' %}
{% load humanize %}
{% block title %}I am a Django God - {{ block.super }}{% endblock %}
{% block content %}
  <h1>Delusions of Grandeur #{{ munber }}</h1>
  <p>They make life EXCITING for {{ thing }}.</p>
  <p>{% if munber %}Number exists!{% else %}booooo{% endif %}</p>
  <p>
    There are {{ munber }} dog{{ munber|pluralize }} and {{ single|apnumber }}
    ocelot{{ single|pluralize }}.
  </p>
{% endblock %}

这是models.py:

from __future__ import unicode_literals

from django.db import models

# Create your models here.
class Thing(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField()
    slug = models.SlugField(unique=True)

这是views.py:

from django.shortcuts import render

# Create your views here.
def index(request):
    munber = 6
    thing = "Thinnged Nam"
    things = Thing.objects.all()
    return render(request, 'index.html', {
        'munber': munber,
        'single': 1,
        'thing': thing,
        'things':things,
    })

我的错误是

  

请求方法:GET   请求网址:http://localhost:8000/

     

Django版本:1.9.2   Python版本:2.7.11   已安装的应用程序   ['采集',    'django.contrib.admin',    'django.contrib.auth',    'django.contrib.contenttypes',    'django.contrib.sessions',    'django.contrib.messages',    'django.contrib.staticfiles',    'django.contrib.humanize']   已安装的中间件:   [ 'django.middleware.security.SecurityMiddleware',    'django.contrib.sessions.middleware.SessionMiddleware',    'django.middleware.common.CommonMiddleware',    'django.middleware.csrf.CsrfViewMiddleware',    'django.contrib.auth.middleware.AuthenticationMiddleware',    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',    'django.contrib.messages.middleware.MessageMiddleware',    'django.middleware.clickjacking.XFrameOptionsMiddleware']

     

回溯:

     

get_response中的文件“//anaconda/envs/hellovenv/lib/python2.7/site-packages/django/core/handlers/base.py”     149. response = self.process_exception_by_middleware(e,request)

     

get_response中的文件“//anaconda/envs/hellovenv/lib/python2.7/site-packages/django/core/handlers/base.py”     147. response = wrapped_callback(request,* callback_args,** callback_kwargs)

     索引中的

文件“/Users/wogsland/Projects/hellowebapp/collection/views.py”     7. things = Thing.objects.all()

     

异常类型:NameError处的NameError   例外值:全局名称'Thing'未定义

1 个答案:

答案 0 :(得分:4)

您尚未将Thing课程导入views.py文件。假设您的models.pyviews.py位于同一文件夹中,您可以将views.py更新为:

from django.shortcuts import render
from .models import Thing

# Create your views here.
def index(request):
    munber = 6
    thing = "Thinnged Nam"
    things = Thing.objects.all()
    return render(request, 'index.html', {
        'munber': munber,
        'single': 1,
        'thing': thing,
        'things':things,
    })