python - Django - 将不同模型的信息显示到同一个 HTML 模板中

标签 python html mysql django view

我正在使用 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/

相关文章:

Python wsgi 网站扫描器

python - GNU 日期和 Python 时间模块之间测量纳秒的不同结果

PHP 代码在 HTML 输入标记中未正确解析

javascript - HTML5相机拉伸(stretch)图片

mysql - 如何从 'rec_host' 列中检索等于 1 和 0 的所有行以进行回显

python - django 如何使用模板

html - Ruby、Nokogiri : how do i ensure UTF8 throughout nokogiri parsing, erb 模板和编码 HTML 文件

Java MYSQL,搜索时忽略空白字符串

mysql - 从具有 2 个条件的表中删除不需要的行

python - 在 OOP 中使用函数是不好的做法吗?