Django pass variable into template

32,787

Solution 1

You can try something like this

views.py

from django.template.response import TemplateResponse

def testdex(requset, template_name="myapp/inculdes.html"):
    args = {}
    text = "hello world"
    args['mytext'] = text
    return TemplateResponse(request, template_name, args)

inculdes.html

{% extends "myapp/index.html" %}
{% block includes %}
{{ mytext }}
{% endblock includes %}

And make sure you have set path for templates in settings.py

Solution 2

When you do {% block content %}{% endblock content %} you are telling Django that you want to be able to overwrite this section. Please note the word content can be anything to reflect what you want to overwrite.

When you do {{ variable }} you are telling Django that you want to pass a Context. In this example, variable I want to pass is called Title as the key and Portfolio as the value. Context is a dictionary that you pass in views.py like this:

def portfolio_home(request):
    return render(request, 'portfolio/work.html', {'title': 'Portfolio'})

Let's say I want to pass a context (or a variable) into my base template. In this example, I want to pass title in the title tag of the head section of my base template.

In the html file for base.html, you need to have something like this:

<!DOCTYPE html>
<html lang="en">

{% load staticfiles %}

    <head>
        <title>{{ title }}</title>
        ...........
    </head>
</html>

In the urls.py of my project and other apps that I want to pass a title into this, I should create the view like this:

def portfolio_home(request):
    return render(request, 'portfolio/work.html', {'title': 'Portfolio'})

Solution 3

I found out why Django can't pass variables to HTML because;

I didn't have my apps url activated the function/model in views

I feel so embarrasses, for such simple mistakes.

All I need to do is add this code in my apps url

urlpatterns = [
path('', views.timedex, name='timedex'), #need add this 
path('', views.index, name='index'),
]
Share:
32,787
Rookies DJ
Author by

Rookies DJ

Updated on November 10, 2020

Comments

  • Rookies DJ
    Rookies DJ over 3 years

    Hi thank you for helping, I'm poor in coding.

    To point: I'm doing a Django project that pass data form data-base to front-end; but right now i can't even pass anything views of Django into templates, I suspect i'm passing the wrong variable types; please do comment on your thought.

    This is my code on views.py:

    from django.shortcuts import render
    
    def index (requset):
        return render(requset,'myapp/index.html') # link to be able open frountend
    
    def testdex(requset):
        text = "hello world"
        context ={'mytext' : text }
        return render(requset,'myapp/inculdes.html', context)
    

    so my variable will be pass into inculdes where extend to index page

    This my codes on in inculdes.html:

    {% exntends "myapp/index.html" %}
    
    {% block includes %}
    {{ mytext }}
    {% endblock includes %}
    

    this my code on index.html:

    <body>
    {% block includes %} {% endblock includes %}    
    </body>
    

    Thanks again on giving me your time to help me and appreciate it if could write me some code because try fix this for whole week

  • Rookies DJ
    Rookies DJ over 5 years
    I tried; it didn't work, but this same as my code right accept is using templateresponse; i'm not sure maybe you could explain a bit?
  • Rookies DJ
    Rookies DJ over 5 years
    I tried it didn't work, what a {{block.super}} anyway?
  • M.G
    M.G over 5 years
    block.super to add your content after original not override it, please share your urls.py.
  • Rookies DJ
    Rookies DJ over 5 years
    Thank you Gad, this is the urls.py codes; from django.contrib import admin from django.urls import include, path urlpatterns = [ path(r'^admin/$', admin.site.urls), path('', include('myapp.urls'), name = 'myapp'), ]
  • Vaidøtas I.
    Vaidøtas I. over 2 years
    Any reason Django wants it passed by a dictionary and not keyword parameters?