【发布时间】:2019-07-31 21:27:04
【问题描述】:
我正在向现有 Django 项目添加新应用程序,但遇到了一些我无法调试的问题。回调错误是“AttributeError:'QuerySet'对象没有属性'META'”,但我不确定这是怎么回事。我今天已经为不同的 API 对象编写了 4 个这样的应用程序,并且没有任何问题。当我向函数添加 print() 调试消息时,我发现执行似乎在出错之前跳出我的lead_lists 视图函数并进入我的列表视图函数。
这是我的 list.views.py
def list(request):
print("we're in lists view")
lists = List.objects.all()
print("lists saved for context")
context = {'lists': lists}
print("context created")
return render(request, 'list.html', context) # fails here
def load_lists(request):
api_lists = services.get_lists()
print("api lists loaded")
db_list_ids = list(List.objects.values_list('list_id', flat=True)) # jumps out of function here
print("db lists loaded") # this never prints
print(f"db_list_ids: {db_list_ids}")
for api_list in api_lists:
if api_list['id'] not in db_list_ids:
api_list = services.transform_list(api_list)
form = ListModelForm(api_list)
if form.is_valid():
form.save
else:
return HttpResponse(form.errors)
print("exited load loop")
lists = List.objects.all()
print("load lists objects saved")
context = {'lists': lists}
print("load lists context saved")
return render(request, 'load_lists.html', context)
预期的结果是,当我导航到 /list/load 时,它会运行 load_lists 视图函数。这是我从控制台得到的输出。
we're in lists view
lists saved for context
context created
[31/Jul/2019 16:20:32] "GET /list/ HTTP/1.1" 200 2458
api lists loaded
we're in lists view
lists saved for context
context created
Internal Server Error: /list/load/
Traceback (most recent call last):
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\David.Wilcox\ongage-django\ongage\list\views.py", line 19, in load_lists
db_list_ids = list(List.objects.values_list('list_id', flat=True))
File "C:\Users\David.Wilcox\ongage-django\ongage\list\views.py", line 14, in list
return render(request, 'list.html', context)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\template\base.py", line 169, in render
with context.bind_template(self):
File "c:\users\david.wilcox\appdata\local\programs\python\python37-32\Lib\contextlib.py", line 112, in __enter__
return next(self.gen)
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\template\context.py", line 246, in bind_template
updates.update(processor(self.request))
File "C:\Users\David.Wilcox\ongage-django\venv\lib\site-packages\django\template\context_processors.py", line 40, in debug
if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS:
AttributeError: 'QuerySet' object has no attribute 'META'
[31/Jul/2019 16:20:36] "GET /list/load/ HTTP/1.1" 500 103930
我原本以为因为使用了“list”这个词,玩的不是很好,所以我重构并重命名了我的变量,但错误是一样的。
【问题讨论】:
-
你已经覆盖了
list内置函数。
标签: html django python-3.x