视图 chilegaleria.views.AgregarTienda_View 没有返回 HttpResponse 对象。它返回 None 而不是
Posted
技术标签:
【中文标题】视图 chilegaleria.views.AgregarTienda_View 没有返回 HttpResponse 对象。它返回 None 而不是【英文标题】:The view chilegaleria.views.AgregarTienda_View didn't return an HttpResponse object. It returned None instead 【发布时间】:2017-08-01 03:01:32 【问题描述】:我在使用 django 1.10.6 时遇到此错误:
ValueError at /AgregarTienda 视图 chilegaleria.views.AgregarTienda_View 没有返回 HttpResponse 目的。它返回 None 。
这是我的看法
from django.shortcuts import render
from django.http import HttpResponse
from chilegaleria.forms import AgregarTiendaForm
# Create your views here.
def index(request):
return render(request, 'chilegaleria/index.html')
def AgregarTienda_View(request):
if request.method == 'POST':
form = AgregarTiendaForm(request.POST)
if form.is_valid():
form.save()
return redirect('chilegaleria:index')
else:
form = AgregarTiendaForm
return render(request, 'chilegaleria/AgregarTienda.html', 'form':form)
当我添加类 Meta 时: 型号 = DatosTienda 在我的 forms.py 文件中出现此错误:
C:\chilegalerias>manage.py check Traceback(最近一次调用最后一次):文件“C:\chilegalerias\manage.py”,第 22 行,在 execute_from_command_line(sys.argv) 文件“C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management__init__.py”,第 367 行,在 execute_from_command_line utility.execute() 文件“C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management__init__.py”,第 359 行,在执行中 self.fetch_command(subcommand).run_from_argv(self.argv) 文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management\base.py", 第 294 行,在 run_from_argv self.execute(*args, **cmd_options) 文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management\base.py", 第 345 行,执行中 输出 = self.handle(*args, **options) 文件“C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management\commands\check.py” , 第 68 行,在句柄中 fail_level=getattr(checks, options['fail_level']), 文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management\base.py", 第 374 行,检查中 include_deployment_checks=include_deployment_checks,文件“C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\management\base.py”, 第 361 行,在 _run_checks 返回 checks.run_checks(**kwargs) 文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\checks\registry.py", 第 81 行,在 run_checks new_errors = check(app_configs=app_configs) 文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\checks\urls.py", 第 14 行,在 check_url_config 中 返回 check_resolver(resolver) 文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\core\checks\urls.py", 第 24 行,在 check_resolver 中 对于 resolver.url_patterns 中的模式:文件“C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\utils\functional.py”, 第 35 行,在 get 中 res = instance.dict[self.name] = self.func(instance) File "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django \urls\resolvers.py", 第 313 行,在 url_patterns 中 模式= getattr(self.urlconf_module,“urlpatterns”,self.urlconf_module)文件 "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\utils\functional.py", 第 35 行,在 get 中 res = instance.dict[self.name] = self.func(instance) File "C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django \urls\resolvers.py", 第 306 行,在 urlconf_module 中 返回 import_module(self.urlconf_name) 文件“C:\Python27\lib\importlib__init__.py”,第 37 行,在 import_module import(名称)文件“C:\chilegalerias\chilegalerias\urls.py”,第 21 行,在 url(r'^', include ('chilegaleria.urls')),文件“C:\Python27\lib\site-packages\django-1.10.6-py2.7.egg\django\conf\urls__init__.py” , 第 50 行,包括 urlconf_module = import_module(urlconf_module) 文件“C:\Python27\lib\importlib__init__.py”,第 37 行,在 import_module import(名称)文件“C:\chilegalerias\chilegaleria\urls.py”,第 3 行,在 从 chilegaleria.views 导入索引,AgregarTienda_View 文件“C:\chilegalerias\chilegaleria\views.py”,第 3 行,在 从 chilegaleria.forms 导入 AgregarTiendaForm 文件“C:\chilegalerias\chilegaleria\forms.py”,第 4 行,在 类 AgregarTiendaForm(forms.ModelForm):文件“C:\chilegalerias\chilegaleria\forms.py”,第 7 行,在 AgregarTiendaForm 中 类 Meta:文件“C:\chilegalerias\chilegaleria\forms.py”,第 8 行,在 Meta Model = DatosTienda NameError: name 'DatosTienda' is not defined
这是我的 forms.py 文件:
from django import forms
from chilegaleria.models import models
from django.forms import ModelForm
class AgregarTiendaForm(forms.ModelForm):
class Meta:
model = DatosTienda
fields = [
'nombre',
'estado',
'ciudad',
'comuna',
'direccion',
]
labels =
'nombre': 'Nombre de la Tienda',
'estado': 'Region',
'ciudad': 'Ciudad',
'comuna': 'Comuna',
'direccion': 'Direccion',
widgets =
'nombre': forms.TextInput(attrs='class':'form-control'),
'estado': forms.TextInput(attrs='class':'form-control'),
'ciudad': forms.TextInput(attrs='class':'form-control'),
'comuna': forms.TextInput(attrs='class':'form-control'),
'direccion': forms.TextInput(attrs='class':'form-control'),
【问题讨论】:
【参考方案1】:文件“C:\chilegalerias\chilegaleria\forms.py”,第 8 行,元模型 = DatosTienda NameError: name 'DatosTienda' is not defined
您的日志堆栈说您必须在文件 chilegaleria\forms.py 中导入模型 DatosTienda
还要检查你的视图,因为如果你收到一个 GET 请求,你的表单没有被定义,所以你会在分配错误之前得到一个引用。
【讨论】:
我修复了它,我有缩进错误,而且我没有导入我的模型 很高兴知道!那么你应该接受这个答案是正确的。还可以尝试查看 PEP8 以获得更简洁的代码。以上是关于视图 chilegaleria.views.AgregarTienda_View 没有返回 HttpResponse 对象。它返回 None 而不是的主要内容,如果未能解决你的问题,请参考以下文章
word中一共有几种视图方式,并且如何区分几种视图关系,如:页面视图、大纲视图等等