Rookies DJ
Rookies DJ

Reputation: 552

Django pass variable into template

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

Upvotes: 17

Views: 58059

Answers (4)

Ajay Kumar
Ajay Kumar

Reputation: 1665

You can try something like this:

views.py

from django.template.response import TemplateResponse

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

includes.html

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

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

Upvotes: 23

azmirfakkri
azmirfakkri

Reputation: 601

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'})

Upvotes: 15

Rookies DJ
Rookies DJ

Reputation: 552

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'),
]

Upvotes: 3

M.G
M.G

Reputation: 11

Add {{block.super}} before {% endblock includes %}

Upvotes: 0

Related Questions