I'm a newbie experimenting with Flask/Jinja2 and SQL-Alchemy.
I have a question about html output when using Jinja2 templating.
My view function looks like this:
from app import app
from flask import render_template
from init_database import init_db
from app.models import Provider
#app.route('/update')
def update():
provider = Provider.query.get_or_404(1)
return render_template("update.html", provider=provider)
And my template looks like this:
{% extends "layout.html" %}
{% block content %}
<div class="page">
<ul>
{ % for provider in prov %}
<li>
{{ provider.nzbprovider }}
{{ provider.rssfeed }}
</li>
{ % endfor %}
</ul>
</div>
{% endblock %}
When I run the app, the out looks like this in the browser:
{ % for provider in prov %}
Peter Johnson
Michael Manning
{ % endfor %}
Why does it display the Jinja tags? What have I forgotten?
You have spaces in the Jinja tag delimiters: Change { % (with a space) to {% (with no space)
Related
I am running a db query with sqlalchemy and then pushing this output into an HTML page with flask and python. However, the output is formatted as a list, and I am trying to remove the quotes around it and format it nicer.
# views.py
#buyers_blueprint.route('/welcome')
#login_required
def welcome_user():
user = current_user
events = db.session.query(Events.eventname)
return render_template('welcome.html', user=user, events=events)
The welcome.html page is simple and looks like this
welcome.html
{% extends "base.html" %}
{% block content %}
<div class="jumbotron">
<h1>Events Dashboard</h1>
<p>{{user}}! You are logged in.<br>
Here is a list of your events</p><br>
<p>
{% for event in events %}
<li>{{event}}</li>
{% endfor %}
</p>
</div>
{% endblock %}
The output looks like this.
('Event 1',)
('Event 2',)
How do I remove the quotes, and comma to format it so it looks nicer? Thank you so much.
So, thanks to help from others, I came up with an answer. In the HTML, I just needed to ask for the first in tuple.
welcome.html
{% extends "base.html" %}
{% block content %}
<div class="jumbotron">
<h1>Events Dashboard</h1>
<p>{{user}}! You are logged in.<br>
Here is a list of your events</p><br>
<p>
{% for event in events %}
<li>{{event[0]}}</li>
{% endfor %}
</p>
</div>
{% endblock %}
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 %}
I would like to group my templates into categories and then just include 1 templates instead of multiple. In simple words when I import a template I would like its imports to be imported as well.
Lets imagine I have a template a.jinja:
{% import 'b.jinja' as b %}
{% import 'c.jinja' as c %}
And template b.jinja and c.jinja are similar containing simple macro:
{% macro macroB() -%}
macroB
{%- endmacro %}
I have also a template test.jinja importing above:
{% import 'a.jinja' as a %}
{{ a.b.macroB() }} {# <- this does not work now #}
Is it possible to make this {{ a.b.macroB() }} work?
I have a base.html which i extends to other pages also. In few pages , images are displayed but in few it does not. other than images , everything like header , section are displayed.
{% load staticfiles %}
some more --like header , section
<footer>
<div id="footer">
{% block footer %}
<img src ="../../static/blog/images/git.png">
<p>© 2016 shankar.</p>
{% endblock %}
</div>
My template file
{% extends 'blog/base.html' %}
{% block content %}
<h1>Articles for {{ year }}</h1>
{% for article in article_list %}
<h4>{{ article.headline }}</h4>
<h5>Posted by <strong>{{ article.reporter }}</strong>
on {{article.pub_date|date:"F j, Y"}}</h5><hr/>
{% endfor %}
{% endblock %}
url `
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'article/(?P<year>[0-9]{4})/$', views.year_archive, name='year_archive'),
url(r'article/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive, name='month_archive'),
url(r'(?P<article_id>[0-9]+)/$',views.article_detail,name='article_detail'),
url(r'^comment/(?P<article_id>[0-9]+)/$' ,views.comment,name='comment'),
url(r'^contact',views.contact,name='contact'),
]`
views
ef year_archive(request,year):
#year=str(pub_date)[0:4]
year=year
try:
article_list = Article.objects.filter(pub_date__year=year)
except Article.DoesNotExist:
raise Http404("Article does not Exists")
context = {'year':year, 'article_list':article_list}
return render(request, 'blog/year_archive.html',context)
It's because you're not using the correct src. You should let the static function handle the static files. When the url changes ../../ will not be correct anymore, depending on the path.
You should configure the static directory in your settings.py file and then reference your image like this:
<img src ="{% static 'blog/images/git.png' %}"></a>
You're loading staticfiles but you never actually use it, you should use the static template tag
"../../static/blog/images/git.png"
should be
{% static 'blog/images/git.png' %}
You should also use the url template tag..
I'm trying to use inheritance in jinja2 on google app engine. But I haven't managed to do it so far. Could you please point me to what I am doing wrong?
Here is my base.html:
{{text}}
{% block title %}
Failure
{% endblock %}
this template is extended by the title.html:
{% extends "base.html" %}
{% block title %}
World!!
{% endblock %}
Both templates are in the same directory /templates/wiki.
This is how I load templates and render base.html:
import os
import jinja2
import webapp2
template_dir = os.path.join(os.path.dirname(__file__), '../templates/wiki')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True)
class MyHandler(webapp2.RequestHandler):
def get(self):
templ = jinja_env.get_template('base.html')
self.response.out.write(templ.render(text = 'Hello,'))
The supposed output is
Hello, World!!!
but I can get just:
Hello, Failure
Jinja2 version is 2.6.
You have to render title.html instead of base.html.