gpt4 book ai didi

python - Django 'global name' 错误

转载 作者:行者123 更新时间:2023-11-28 22:38:45 24 4
gpt4 key购买 nike

我正在设置我的第一个 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 %}

这是模型.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,
})

我的错误是

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

最佳答案

您还没有将 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,
})

关于python - Django 'global name' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35395046/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com