Django 'global name' error

I am setting up my first Django app. I am having some trouble getting my database model to work.

Here is my template:

{% 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 %}

Here is 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)

Here is 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,
    })

My error is

Request Method: GET Request URL: http://localhost:8000/

Django Version: 1.9.2 Python Version: 2.7.11 Installed Applications: ['collection', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.humanize'] Installed Middleware: ['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']

Traceback:

File "//anaconda/envs/hellovenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 149. response = self.process_exception_by_middleware(e, request)

File "//anaconda/envs/hellovenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 147. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/Users/wogsland/Projects/hellowebapp/collection/views.py" in index 7. things = Thing.objects.all()

Exception Type: NameError at / Exception Value: global name 'Thing' is not defined


You haven't imported your Thing class into your views.py file. Assuming your models.py is in the same folder as views.py , you could update your views.py to:

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,
    })
链接地址: http://www.djcxy.com/p/55916.html

上一篇: 在Twython中获取访问令牌错误401

下一篇: Django'全球名称'错误