B
B
bimka2021-11-11 20:52:32
Django
bimka, 2021-11-11 20:52:32

Why doesn't the dictionary (context) pass to the template?

I want to display the number of page views. I can’t understand why the elements of the context dictionary are not displayed in the template.

views.py:

from django.shortcuts import render

def index(request):
    num_visits = request.session.get('num_visits', 0) + 1
    request.session['num_visits'] = num_visits
    return render(request, 'hello/main.html', context = {"num_visits": num_visits, "ola": "lala",})


URLs.py:
from django.urls import path

from . import views

app_name = 'hello'

urlpatterns = [
    path('', views.index),
]


main.html:
{% if ola %}<p>ola is running </p>{% else %}<p>It didn't work</p>{% endif %}
view count = {{ num_visits }}


It does not give any errors:
618d5ec8074cb190212918.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2021-11-11
@deepblack

At first glance, this design should work.
On the local computer, I suppose there is no problem,
Does it only appear on Pythonanywhere?
Throw a link to the github turnip from which the deployment to pythonanywhere takes place

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question