I am a rookie in Flask, and try to create dynamic input fields. I came across a solution where I can create the template in Flask and then I have to iterate in my .html file with the following code:
{% from 'your_form_template.jinja' import forms %}
{% for address_entry_form in form.addresses %}
{{ address_entry_form.hidden_tag() }}
{# Flask-WTF needs `hidden_tag()` so CSRF works for each form #}
{{ forms.render_field(address_entry_form.name) }}
{% endfor %}
I can't figure out what do I need to put in the first line instead of 'your_form_template.jinja'
I only have a simple app structure:
app
|
Templates--
| |
| Index.html
app.py
Your top line wouldn't be in your html, you'd pass the variable addresses to jinja. So in app.py import your forms module and:
#app.route('/', methods=('GET','POST'))
def index():
#stuff you're doing in index
return render_template('index.html', adresses=forms.addresses) #assuming forms.addresses here is actually a list of forms
Then the html would have
{% for address_entry_form in adresses %}
....
Related
I have rendering one string in django template but in django template, I want to convert it into integer. I have tried many thing like |add:to_int but it doesn't work.
#Vaibhav Mishra,
Does it work for you? https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#add
Like: {% value|add:"0" %}
If it does not work for you: then create a custom template filter and register it:
YOUR_APP_FOLDER/
__init__.py
models.py
templatetags/
__init__.py
your_app_name_extras.py [any valid filename.py]
views.py
in your_app_name_extras.py
from django import template
register = template.Library()
#register.filter()
def to_int(value):
return int(value)
In your template file, you need to put the code:
{% load your_app_name_extras %}
{{ value|to_int }}
For more details:
https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/
Need to convert a string to int in a django template
You can do like that:
{% if string_value == int_value|stringformat:'s' %} {% endif %}
When I type {{ settings }} on a template and open it with Mezzanine, it shows me {u'MEZZANINE_ADMIN_PREFIX': u'grappelli/'}. I'm trying to access settings.BLOG_SLUG, but cannot get that setting to appear in the template. Here's a small snip of what my template looks like.
{% load mezzanine_tags keyword_tags i18n %}
{% block main %}
{{ settings }}
{% endblock %}
How do I get my template to display the string stored in setting.BLOG_SLUG?
you have to pass settings.BLOG_SLUG from your view than you would be able to display it in html.
for example you have written line settings.py file like
BLOG_SLUG = 'something'
than you can retrieve it in your view as
SETTINGS.BLOG_SLUG
and you can pass it from your view to html
I'm using Python with Flask and Jinja2 and I'm trying to implement a sidebar. In the HTML pages I have got this:
{% include "sidebar.html" %}
What I want in the sidebar file is to have a block of the latest users. To do that I need to get the results from the server for the sidebar.html file. But where should I write the code for that in the python file?
The way you could implement this functionality is by creating a Jinja variable on the python side:
app = Flask(__name__)
app.jinja_env.globals.update({
'latest_users': get_latest_users()
})
def get_latest_users() {
return ['Mark', 'Jane', 'Sally']
}
The variable latest_users can now be accessed from any Jinja template simply by doing:
{% for user in latest_users %}
<p>{{ user }}</p>
{% endfor %}
You can pass variables to the templates. When you use include your variables still can be used in this included part too:
#app.route('/')
def index():
users = ['user1', 'user2']
debug = False
render_template('index.html', users=users, debug=debug)
# index.html
{% include "sidebar.html" %}
#sidebar.html
{% for user in users %}
<p>{{ user }}</p>
{% endfor %}
{{debug}}
As the cookbook of webpy and jinja2, I can use webpy's form or jinja2 well independently. However when I try to combining both in a template file like below, it does not work:
Template file:
$def with(form)
{% extends 'layout.html' %}
{% block maincontents %}
<h1>User</h1>
<form method="post">
$:form.render()
</form>
{% endblock %}
Part of python code:
render = render_jinja(
'templates',
encoding='utf-8',
)
class test:
def POST(self):
pass
def GET(self):
f = user_form()
return render.test(f)
$:form.render() is the Templetor rendering instruction, taken verbatim from the docs, I presume.
I believe you should use Jinja2 syntax, something like
<form method="post">
{{ form.render() | safe }}
</form>
Disclaimer: I haven't actually tested the snippet above.
In django I have a view that fills in a template html file but inside the html template I want to include another view that uses a different html template like so:
{% block content %}
Hey {{stuff}} {{stuff2}}!
{{ view.that_other_function }}
{% endblock content %}
Is this possible?
Yes, you need to use a template tag to do that. If all you need to do is render another template, you can use an inclusion tag, or possibly just the built in {% include 'path/to/template.html' %}
Template tags can do anything you can do in Python.
https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/
[Followup]
You can use the render_to_string method:
from django.template.loader import render_to_string
content = render_to_string(template_name, dictionary, context_instance)
You'll either need to resolve the request object from the context, or hand it in as an argument to your template tag if you need to leverage the context_instance.
Followup Answer: Inclusion tag example
Django expects template tags to live in a folder called 'templatetags' that is in an app module that is in your installed apps...
/my_project/
/my_app/
__init__.py
/templatetags/
__init__.py
my_tags.py
#my_tags.py
from django import template
register = template.Library()
#register.inclusion_tag('other_template.html')
def say_hello(takes_context=True):
return {'name' : 'John'}
#other_template.html
{% if request.user.is_anonymous %}
{# Our inclusion tag accepts a context, which gives us access to the request #}
<p>Hello, Guest.</p>
{% else %}
<p>Hello, {{ name }}.</p>
{% endif %}
#main_template.html
{% load my_tags %}
<p>Blah, blah, blah {% say_hello %}</p>
The inclusion tag renders another template, like you need, but without having to call a view function. Hope that gets you going. The docs on inclusion tags are at: https://docs.djangoproject.com/en/3.0/howto/custom-template-tags/#inclusion-tags
Using your example and your answer to Brandon's response, this should work for you then:
template.html
{% block content %}
Hey {{stuff}} {{stuff2}}!
{{ other_content }}
{% endblock content %}
views.py
from django.http import HttpResponse
from django.template import Context, loader
from django.template.loader import render_to_string
def somepage(request):
other_content = render_to_string("templates/template1.html", {"name":"John Doe"})
t = loader.get_template('templates/template.html')
c = Context({
'stuff': 'you',
'stuff2': 'the rocksteady crew',
'other_content': other_content,
})
return HttpResponse(t.render(c))
Someone created a template tag that loads a view. I've tried it, and it works. The advantage of using that template tag is that you don't have to rewrite your existing views.