Yoann Pageaud
Yoann Pageaud

Reputation: 519

Django - display informations from different models into the same HTML template

I am using Django with a MySQL database So I know that that 1 view in views.py is associated to 1 HTML template and that I cannot associate 2 different views to the same template. I tried to create a view "about" containing some stats about my database. Unfortunately it doesn't work.

Here is what I've done so far for my view "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)

And here is the Error returned in my terminal:

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

I checked in the django shell the following commands are working as they should:

Pdb.objects.count()
StructSec.objects.count()

The syntax of my view seems good to me...

Someone suggested that I should add "poll_id" to my view function like this:

def about(request,poll_id):

What is this "poll_id" ? Where does this come from ?

If someone has any idea about what is going on I would really appreciate a detailled answer/solution since I am a beginner in Django.

Upvotes: 0

Views: 49

Answers (1)

JacobIRR
JacobIRR

Reputation: 8946

You are missing commas:

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

Upvotes: 1

Related Questions