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请求URL:http:// localhost:8000 /

Django版本:1.9.2 Python版本:2.7.11安装的应用程序:['collection','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 149中的文件“//anaconda/envs/hellovenv/lib/python2.7/site-packages/django/core/handlers/base.py”。response = self.process_exception_by_middleware(e,request)

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

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

异常类型:NameError at /异常值:全局名称'Thing'未定义


您尚未将您的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,
    })
链接地址: http://www.djcxy.com/p/55915.html

上一篇: Django 'global name' error

下一篇: How to upload csv file using django project