django-templates

Django Templates: Comparing current url with {% url xyz %}

耗尽温柔 提交于 2019-12-02 20:48:49
I am trying change the active selection of my navigation links based on the current page where the user is at. I am trying to do omething like this: <li {% if request.get_full_path == {% url profile_edit_personal %} %}class="current"{% endif %}><a href="{% url profile_edit_personal %}">Personal Details</a></li> Alternatively, I know I could define do something like this: <li class="{% block current %}{% endblock %}"><a href="{% url profile_edit_personal %}">Personal Details</a></li> and add a {% block current %}current{% endblock %} to each of the relevant templates but I would prefer

Django and Mustache use the same syntax for template

点点圈 提交于 2019-12-02 20:25:00
I try to smuggle HTML template in the HTML for mustache.js, however the django template engine remove all the placeholders that should be output as-is to the front-end The template is included in HTML in this way: <script type="text/x-mustache-template" data-id="header_user_info"> <div id="header_user_info"> <div id="notification">0</div> <a href="#">{{username}}</a> </div> </script> and I can get the HTML template by running $(el).html(), and generate html by using Mustache.to_html(temp, data); I could put all the template into another static file and serve from CDN, but then it would be hard

Migrate url tags to django 1.5

风流意气都作罢 提交于 2019-12-02 20:24:53
I'm trying to migrate an old django application to django 1.5, There are 745 urls in different html files as this way: {% url url_name %} If I'm not wrong, this was deprecated and can't be used anymore from django 1.5 (as said here ), and I have to transform all of them into: {% url 'url_name' %} Any idea to do this without going crazy? Maybe, some kind of script, I dont know... I can't imagine a way to do it with replace in path. I'm probably missing something obvious. NOTE: This command is destructive. Use version control or backup your templates directory before running it. You can use sed.

Django media URLs in CSS files

我是研究僧i 提交于 2019-12-02 20:09:26
In django templates, it's common to do the following: <img src="{{ MEDIA_URL }}/img/someImage.jpg"> How would you accomplish this in a CSS file which is not served as a template? .someClass { /* can't do this this */ background: url("{{ MEDIA_URL }}/img/someImage.jpg"); /* either this */ background: url("http://media.domain.com/img/someImage.jpg"); /* or this */ background: url("/django_static_media/img/someImage.jpg"); /* can't do both... what to do? */ } I need the ability to serve my files either from the media subdomain, or during offline work and serve them directly as a django static

Limit number of characters with Django Template filter

人走茶凉 提交于 2019-12-02 20:00:59
I am trying to output the first 255 characters of a description on a list of items and am looking for a method to get that. Example: I have a variable that contains 300 or so characters. I call that variable like this, {{ my_variable|characterlimit:255 }} and it would return only the first 255 characters of that variable. If this tag doesn't exist, I will simply create it (and suggest that it goes into django), but I wanted to make sure it didn't before I took the time to do that. Thanks! heckj If the "my_variable" is a string, you can take advantage of the slice filter , which treats the

Placing markers on Google Map With Django

空扰寡人 提交于 2019-12-02 19:49:35
I'm trying to place markers based on the latitude and longitude stored in a model on a Google Map using the API and HTML5 geolocation. The issue is how to loop through the lat/lon info for each object stored within JavaScript tags using template keywords, which I don't believe can be done in Django. I found a similar question here Adding Google Map Markers with DJango Template Tags in Javascript which I mildly modified and placed within a template – not a separate script file – but it doesn't seem to work: function loadMarkers(){ {% for story in stories %} var point = new google.maps.LatLng({

Use of “if” in template with custom template tag with multiple arguments

廉价感情. 提交于 2019-12-02 19:21:16
问题 I wrote a custom template tag to query my database and check if the value in the database matches a given string: @register.simple_tag def hs_get_section_answer(questionnaire, app, model, field, comp_value): model = get_model(app, model) modal_instance = model.objects.get(questionnaire=questionnaire) if getattr(modal_instance, field) == comp_value: return True else: return False In my template I can use this tag as follows: {% hs_get_section_answer questionnaire 'abc' 'def' 'ghi' 'jkl' %} The

Jinja2 template super functions not rendered with django

时间秒杀一切 提交于 2019-12-02 18:45:44
问题 I have two very simple templates like index.html: <html> <head> </head> <body> {% block content %}hello{% endblock %} </body> </html> and details.html {% extends "index.html" %} {% block content %}{{ super() }} world{% endblock %} but when i render a view with details.html i get this error Could not parse the remainder: '()' from 'super()' do i need some import somewere? (templates are rendered properly until i use the super() function) 回答1: Django 1.7 and earlier do not support Jinja

how do I replace no username with a string of choice in Django

橙三吉。 提交于 2019-12-02 18:29:30
问题 I would like to have 'No user assigned' when there is no user to the ticket. I did this to my model but I don't see any changes def __unicode__(self): print 'gets here****' if not self.assigned: return 'No user assigned' else: return self.assigned where assigned is the user assigned for a ticket. IS ther any other way to do this? I have my template and view like this: {% for ticket in tickets %} <tr> <td>{{ ticket.title }}</td> <td> {% for user in ticket.assigned.all %} {{ user.email }}{% if

How to include templates dynamically in Django using “include” tag

回眸只為那壹抹淺笑 提交于 2019-12-02 18:15:20
I have 10 html files with the names 1.html, 2.html ..etc What I want is according to a variable, a certain file should be included in the template. e.g. {% if foo.paid %} {% include "foo/customization/{{ foo.id }}.html" %} {% endif %} Is this possible ? Cause the foo.id is not being translated before the includes tag works. As a result its giving a error. How can this issue be handled in a different way ? Should I create a custom template tag for this ? You can do it with add filter and with statement . {% if foo.paid %} {% with template_name=foo.id|stringformat:"s"|add:".html" %} {% include