muzykeq1
muzykeq1

Reputation: 3

HTML template doesn't see the variable passed from views

I am trying to pass a variable from views.py to my login.html template.

This is my views.py

from django.shortcuts import render

# Create your views here.
def home(request):
    numbers = [1, 2, 3, 4, 5]
    context = {
        'numbers': numbers
    }

    return render(request, 'accounts/login.html', {'title': 'Login'}, context)

and this is my login.html

{% extends 'accounts/base.html' %}
{% block content %}
    <h1>Welcome</h1>
    <h5 class="text-muted">Please log in to continue</h5>
    <button href="#" class="btn btn-outline-primary mt-1">Log in</button>
    {{ numbers }}
{% endblock content %}

also, this is my urls.py file inside of my application

from . import views
from django.urls import path

urlpatterns = [
    path('', views.home, name="home"),
    path('home/', views.home, name="home"),
]

I expect the list [1, 2, 3, 4, 5] to show on login.html, when the address is localhost:8000/myappname/home

Upvotes: 0

Views: 30

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599530

You have two dictionaries for some reason. You only need one, which should contain all the values you want to pass.

def home(request):
    numbers = [1, 2, 3, 4, 5]
    context = {
        'numbers': numbers,
        'title': 'Login'
    }

    return render(request, 'accounts/login.html', context)

Upvotes: 4

Related Questions