django-templates

Django pass object from view to next for processing

点点圈 提交于 2019-12-03 13:24:24
If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to create a table. How would you pass the created object in the first view to the next view so you can use it in the second view's template I'd appreciate any help you can share One approach is to put the object into a session in your first view, which you could then retrieve from the request.session in the second view. def first_view(request): my_thing = {'foo' : 'bar'} request.session['my_thing'] = my_thing return render

Django templates: how to avoid empty lines with include and load?

回眸只為那壹抹淺笑 提交于 2019-12-03 13:06:59
In a template, I use the following code: {% load i18n %} {% include "header.html" %} {% include "top_bar.html" %} But this produces several carriage returns ( \n ) as I separate every include and load by a carriage return. I don't want to concatenate inclusions like this: {% include "header.html" %}{% include "top_bar.html" %} Because it's unreadable. Is there a way to avoid these generated new lines ? Well after some researches, I found this thread where a patch is submitted and currently being reviewed. This validation step takes too much time regarding my project. Here is a django module I

Resetting cache for Django cached template loader

安稳与你 提交于 2019-12-03 13:05:48
Django 1.2 introduced a new template loader, that stores data in cache ( django.template.loaders.cached.Loader ). Unfortunatly, i failed to find any info on how the cache is invalidated and when and how it is reset. I want to use this on my server, but i'm not sure, that it would reset on django restart (that would be enough for me). By digging into django's source, you could find the template loaders for current server instance are stored at django.template.loader.template_source_loaders . When you're using cached loader, there would be only one loader out there. So you can get it and calls

In Jinja2, how can I use macros in combination with block tags?

烈酒焚心 提交于 2019-12-03 12:58:23
I'm a front end developer, and I've been trying to get a hang on using Jinja2 effectively. I want to tweak a current site so it has multiple base templates using inheritance, it fully uses block tags to substitute content and override it, and uses macros to support passing of arguments. My base template contains this code (edited for simplicity): <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> {% from "foo.html" import macro1, macro2, macro3 %} {% macro base_template(title=none, arg2=none, urls={}, arg3=false) %} <html> <title>{{ title }} | Site.com<

Django Templates - Printing Comma-separated ManyToManyField, sorting results list into dict?

时光总嘲笑我的痴心妄想 提交于 2019-12-03 12:54:43
I have a Django project for managing a list of journal articles. The main model is Article . This has various fields to store things like title of the article, publication date, subject, as well as list of companies mentioned in the article. ( company is it's own model). I want a template that prints out a list of the articles, sorted by category, and also listing the companies mentioned. However, I'm hitting two issues. Firstly, the company field is a ManyToMany field. I'm printing this successfully now, using the all iterable, thanks to this SO question =). (Curious though, where is this all

Django CMS Page Title Doesn't Render

…衆ロ難τιáo~ 提交于 2019-12-03 12:47:38
I'm currently working on a project that uses django-registration and Django CMS. When display the pages that implement django-registration my page titles do not render. Currently have <title>{% page_attribute page_title %}</title> in base.html which all my templates inherit from. In the pages that do not use django-registration the titles display just fine, but django-registration display as <title></title> My pages are all created within the CMS and everything else is rendering correctly. If I set the title explicitly within the template, the title will render, but I'd rather have it set

Django - Template tags in javascript and css files

假如想象 提交于 2019-12-03 12:29:29
Is there any way to add template tags into javascript and css files? I would use it for anything from passing in urls to media url links (image paths, etc) to conditional javascript based on user permissions. I just had a thought that maybe I can serve it up as if it were a template but have the url as my javascript file. Is that the only way to do somethign like this? If so, it probably wouldn't work with my media generator, so I'd probably want a better solution if there was one out there. How about defining the JavaScript variables and CSS attributes from within your Django HTML template,

django template and dictionary of lists

强颜欢笑 提交于 2019-12-03 12:19:49
I'm using django's template system, and I'm having the following problem: I pass a dictionary object, example_dictionary, to the template: example_dictionary = {key1 : [value11,value12]} and I want to do the following: {% for key in example_dictionary %} // stuff here (1) {% for value in example_dictionary.key %} // more stuff here (2) {% endfor %} {% endfor %} However, this does not enter on the second for loop. Indeed, if I put {{ key }} on the (1), it shows the correct key, however, {{ example_dictionary.key }} shows nothing. In this answer , someone proposed using {% for key, value in

Django string concatenation inside template tag best practice

久未见 提交于 2019-12-03 12:09:59
I'm trying to concatenate some strings to format a URL inside my template tag, but I don't find an elegant way. So far, what I have is: {% button "Activate" "http://" site.domain url 'registration_activate' activation_key %} Is there any best practice to make it a bit more "readable"? Thanks a lot Zahid Sumon You can concatenate two strings in Django template as follows: {{"First String "|add:"Second String"}} Just replace the two strings with your own variable. What I use when I want to concatenate strings in Django templates from variables (examples taken from my own code, tell me if you

use slugify in template

*爱你&永不变心* 提交于 2019-12-03 11:45:25
I want to have SEO-friendly URL ,my current url in urls.py : (ur'^company/news/(?P<news_title>.*)/(?P<news_id>\d+)/$','CompanyHub.views.getNews') I use it in template: {% for n in news %} <a href="{% url CompanyHub.views.getNews n.title,n.pk %}" >{{n.description}}</a> {% endfor %} I use news_id to get news object with that PK . I want to convert this url: ../company/news/tile of news,with comma/11 to: ../company/news/tile-of-news-with-comma/11 by doing some thing like this in template: {% for n in news %} <a href="{% url CompanyHub.views.getNews slugify(n.title),n.pk %}" >{{n.description}}</a>