23π
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
14π
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'})
- Appropriate choice of authentication class for python REST API used by web app
- How do I add custom actions to a change model form in Django Admin?
- Django Override Admin change_form.html Template β display associated model in template
- How do you get Django to make a RESTful call?
- Reset SQLite database in Django
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'),
]
- How to append pages of data using jQuery and Django pagination?
- How to get Interdependent dropdowns in django using Modelform and jquery?
- Django Multiple Databases Fallback to Master if Slave is down
- Django Rest Framework 3.1 breaks pagination.PaginationSerializer
- Django unique together constraint failure?
- How to send success message if we use django generic views
- In Django how do I notify a parent when a child is saved in a foreign key relationship?
- Django β How to send a success message using a UpdateView CBV
- What does it mean for an object to be unscriptable?
- Django logging β django.request logger and extra context