Flask, jinja2 - Dynamically append templates one after another - html

I am building a chatbot. There are few child templates like login.html, messages.html, transaction.html, etc. I want to append these templates in base.html dynamically. I am extending base.html in all these templates. My problem is only one template is rendered at a time. Is there any solution for appending these templates one after another? I have used {%include%} but it's a static approach. I need dynamic.
printer.py looks like -
#app.route('/respond', methods=['GET','POST'])
def respond_def():
message = request.form['message_input']
if message == "l":
return render_template('printer/login.html')
elif message == "t":
return render_template('printer/transactionID.html')
base.html looks like -
//some code here
<li>
{% block template %}{% endblock %}
</li>
//some code here
message.html looks like -
{% extends "base.html" %}
{% block template %}
<div> Message template called </div>
{% endblock %}

I resolved it.
I made a list of templates in printer.py and then appended those templates in base.html whenever user asked for it.
printer.py
dictionary = []
// append name of template in this whenever needed.
return render_template('printer/base.html', dictionary=dictionary)
base.html
{% for d in dicts %}
{% set template = 'printer/' + d + '.html' %}
// can add conditions for different templates
{% include template %}
{% endfor %}

Related

Check for a specific url and render jinja2 / html

How can check for a specific URL in jinja2 within html files and then render different html files based on what URL I am currently at?
For eg.
# if {{ request.path == /siteID/ }}
{% include "file1.html" %}
# if {{ request.path == /siteID/abcde/ }}
{% include "file2.html" %}
Current logic I have which I feel is not very good:
<!-- ..... bunch of html lines .... -->
{% if request.path|length > 8 and request.path.startswith('/siteID/') and request.path[8:].strip('/').split('/')|length == 1 %}
{% include "file2.html" %}
{% else %}
{% include "file1.html" %}
{% endif %}
<!-- ..... bunch of html lines .... -->
Also how do I scale this if I want to do something in the future like:
# if {{ request.path == /siteID/abcde/uvwxyz }}
{% include "file3.html" %}
Since you want to scale this later, don't do this in the template. Put the logic into a view function, then pass a variable to the render function that stores the name of the template you want to include. E.g.:
def the_view(request):
if request.path == '/siteID/abcde/':
template = 'file1.html'
elif request.path == '/siteID/fgh/':
template = 'file2.html'
else:
template = 'file_default.html'
return render(request, 'base.html', {'template_to_include': template})
Then in your base.html template put this dynamic import line somewhere:
{% include template_to_include %}

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
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
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'})
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'),
]
Add {{block.super}} before {% endblock includes %}

Use multiple block and views in django templates

I am new to django, and I see that you could create templates that you could populate in views.
You could also create some basic.htm that everyone extends...
Let say I have two pages: (django demo)
List of all questions
Detail of the question.
Now I would like to create "one page" that have first view as sidebar and another as a "detail- right" view.
I would like that on clicking on the list in sidebar change right vies.
It would be nice, if I could use different views (inside views.py) for loading separate templates.
I wish for base html would be something like this :
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Makro Zadravec</title>
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'demo/css/bootstrap.min.css.css' %}" />
</head>
<body class="body" style="background-color: #f6f6f6">
<div>
<h1> This is title</h1>
</div>
<div>
{% block sidebar %}
{% endblock %}
</div>
<div>
{% block content %}
{% endblock %}
</div>
</body>
</html>
And then I would have template for blocks:
content
sidebar
in separate views.
Apart from styling here is the logic you can follow this
as you said you already have template which already loaded list of question then your base view would return all the question object as queryset.
First of all, you don't need to write separate template because you can handle this
{% block content %}
// this will load list of all question
{% endblock %}
{% block detail %}
// here display detail of question
{% endblock %}
create two urls to hit one without kwargs and other with kwargs (if you use django version < 2.0 you need to use url instead of path)
path('target/', QuestionView.as_view(), name='target-list'),
path('target/<int:pk>', QuestionView.as_view(), name='target-detail')
Now in view file you just need to handle the kwargs:
class QuestionView(TemplateView):
template_name = "template.html"
model = Question
def get_context_data(self, **kwargs):
context = super(QuestionView, self).get_context_data(**kwargs)
context['question_list'] = #your queryset to list question#
pk = kwargs.get('pk', None) # this will return pk (id of question) if hit url otherwise None
if pk:
context['question_detail'] = #your queryset to get question-detial#
return context
so if you hit url with kwargs it will return both list of question and detail with it as context in template which you can access as below:
{% block content %}
# access question_list to list out all question
{% endblock %}
{% block detial %}
{% if question_detail %} // if question_detail context passed from view
# add detail of question by accessing question_detail
{% endif %}
{% endblock %}
You can do this based on one view.
Create variable called 'question' in your view which at start is defined as an empty string. Then if someone click on some question on sidebar block you can make url with parameter with id of question to show details ('/page/?question=1') or you can store this id in session, as you want. And call same view another time. Then in view you check if you get parameter in url or if it's stored in session and if it's true you assign question object (which you get by id) to variable 'question'. Send this variable to template. In template you render sidebar always and check if variable 'question' is not equal to empty string then you render details for question.
Something like this:
{% block sidebar %}
...
{% endblock %}
{% if question != '' %}
{% block content %}
...
{% enblock %}
{% endif %}

How to pass a frontmatter value into a for loop

I want to use a value in my frontmatter to specify a data file to loop through, but I can't get this to work.
I have a data file in _data/sidebars/sidebar1.yml. It contains:
- first
- second
- third
On a page I have this frontmatter:
---
title: My page
sidebar: site.data.sidebar.sidebar1
---
I want to use this code:
{% for entry in page.sidebar %}
* {{entry}}
{% endfor %}
However, this doesn't work. I've tried a number of things (involving assign and capture tags to define the page.sidebar content, but nothing seems to work).
The only thing that works is to do this:
{% if page.sidebar == "site.data.sidebars.sidebar1" %}
{% assign sidebar = site.data.sidebars.sidebar1 %}
{% endif %}
{% for entry in sidebar %}
* {{entry}}
{% endfor %}
However, I would like to avoid this extra code with the if statement, since it's easy to forget this and I would like to automate this more.
Any ideas on how to make this work?
You have a typo in your front matter. It's :
sidebar: site.data.sidebars.sidebar1
not
sidebar: site.data.sidebar.sidebar1
You can even be less verbose.
In your front matter : sidebar: sidebar1
In your code :
{% assign sidebar = site.data.sidebars[page.sidebar] %}
{% for entry in sidebar %}
{{ entry | inspect }}
{% endfor %}

Get Django User group in HTML

I am trying to get Django user's group in HTML for an if tag. This is what I tried:
{% ifequal user.groups.all.0 'ABC' %}
{% endif %}
But this is not working. What other way is there?
Try this:
{% for group in request.user.groups.all %}
{% if group.name == 'ABC' %}{% endif %}
{% endfor %}
Or
{% if request.user.groups.all.0.name == 'ABC' %}{% endif %}
You have to access the current user object from the request context variable. For this, make sure that django.template.context_processors.request is in your template settings.
request.user.groups.all.0 returns a Group model object, so you have to compare against the name field.
I think you will have to use a little Python here. For example, a custom template tag:
#register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
And in your template:
{% if request.user|has_group:"ABC" %}
...
{% endif %}
(Source: http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/)
But maybe you should actually use permissions here.
https://docs.djangoproject.com/en/1.8/topics/auth/default/#authentication-data-in-templates
Edit: Here is a more complete example of the custom template tag:
settings.py:
INSTALLED_APPS = [
...
'yourapp',
...
]
Filesystem:
yourproject/
manage.py
yourproject/
settings.py
wsgi.py
...
yourapp/
__init__.py
templatetags/
__init__.py
yourapp_extras.py
...
yourapp_extras.py:
from django import template
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
return user.groups.filter(name=group_name).exists()
Template:
{% load yourapp_extras %}
{% if request.user|has_group:"ABC" %}
...
{% endif %}
To get a more thorough understanding of this, I highly recommend reading Django's excellent documentation.
<h1>{{ user.groups.all.0 }}</h1>
{% if user.groups.all.0.name == 'Team2' %}
<h1>YES</h1>
{% else %}
<h1>NO</h1>
{% endif %}
Here, the user.groups.all.0 gives you the first group assigned to the user.
For eg. if the logged in user has groups assigned to him as- 'Team2', 'Programmer', 'Beginner'.
Then {{user.groups.all.0}} will print Team2.
I've used it to print and check the logged in user's group in html template.
OR
{% if request.user|has_group:"mygroup" %}
<h1>Has group mygroup</h1>
{% endif %}
Also works fine in django v1.11. This will check if the current user has 'mygroup' assigned to it or not.
However you'll need to add
from django import template
from django.contrib.auth.models import Group
register = template.Library()
#register.filter(name='has_group')
def has_group(user, group_name):
group = Group.objects.get(name=group_name)
return True if group in user.groups.all() else False
inside a group_check.py in below file structure
--app
|templates
|templatetags
|-- group_check.py