我正在使用 Django 和 MySQL 数据库 所以我知道views.py 中的1 个 View 与1 个HTML 模板相关联,并且我无法将2 个不同的 View 与同一个模板相关联。 我尝试创建一个“关于” View ,其中包含有关我的数据库的一些统计信息。 不幸的是它不起作用。
这是我迄今为止为“关于”的观点所做的事情:
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.template import loader
from .models import Pdb, StructSec
def about(request):
namelist = ['Quentin LETOURNEUR', 'Yoann PAGEAUD']
pdbcount = Pdb.objects.count()
structcount = StructSec.objects.count()
context = {
'namelist': namelist
'pdbcount': pdbcount
'structcount': structcount
}
return render(request, 'pdbapp/about.html', context)
这是我的终端返回的错误:
Unhandled exception in thread started by <function wrapper at 0x7f25cf87a9b0>
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 121, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 374, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 361, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "/usr/local/lib/python2.7/dist-packages/django/core/checks/urls.py", line 24, in check_resolver
for pattern in resolver.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 313, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/django/urls/resolvers.py", line 306, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/yoann/Projet_BD_M2BI/PDBWebsite/PDBWebsite/urls.py", line 18, in <module>
from pdbapp.views import *
File "/home/yoann/Projet_BD_M2BI/PDBWebsite/pdbapp/views.py", line 52
'pdbcount': pdbcount
^
SyntaxError: invalid syntax
我在 django shell 中检查了以下命令是否正常工作:
Pdb.objects.count()
StructSec.objects.count()
我的 View 的语法对我来说似乎很好......
有人建议我应该将“poll_id”添加到我的 View 函数中,如下所示:
def about(request,poll_id):
这个“poll_id”是什么?这是从哪里来的?
如果有人知道发生了什么,我真的很感激详细的答案/解决方案,因为我是 Django 的初学者。
最佳答案
您缺少逗号:
context = {
'namelist': namelist,
'pdbcount': pdbcount,
'structcount': structcount
}
关于python - Django - 将不同模型的信息显示到同一个 HTML 模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356868/