Django - 将来自不同模型的信息显示在同一HTML模板中

时间:2017-04-11 21:56:28

标签: python html mysql django view

我正在使用Django和MySQL数据库 所以我知道views.py中的1个视图与1个HTML模板相关联,并且我无法将2个不同的视图关联到同一个模板。 我试图创建一个“about”视图,其中包含有关我的数据库的一些统计信息。 不幸的是它不起作用。

以下是我迄今为止对“关于”的看法所做的事情:

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()

我的观点的语法对我来说似乎很好......

有人建议我应该将“poll_id”添加到我的视图函数中:

def about(request,poll_id):

这是什么“poll_id”?这是从哪里来的?

如果有人对正在发生的事情有任何了解,我会非常感谢一个详细的答案/解决方案,因为我是Django的初学者。

1 个答案:

答案 0 :(得分:1)

您缺少逗号:

context = {
    'namelist': namelist,
    'pdbcount': pdbcount,
    'structcount': structcount
}