Rendering a template variable as HTML
If you don’t want the HTML to be escaped, look at the safe filter and the autoescape tag: safe: {{ myhtml |safe }} autoescape: {% autoescape off %} {{ myhtml }} {% endautoescape %}
If you don’t want the HTML to be escaped, look at the safe filter and the autoescape tag: safe: {{ myhtml |safe }} autoescape: {% autoescape off %} {{ myhtml }} {% endautoescape %}
If it’s a value you’d like to have for every request & template, using a context processor is more appropriate. Here’s how: Make a context_processors.py file in your app directory. Let’s say I want to have the ADMIN_PREFIX_VALUE value in every context: from django.conf import settings # import the settings file def admin_media(request): # return … Read more
you can filter the waypoints by passing the user to the form init class waypointForm(forms.Form): def __init__(self, user, *args, **kwargs): super(waypointForm, self).__init__(*args, **kwargs) self.fields[‘waypoints’] = forms.ChoiceField( choices=[(o.id, str(o)) for o in Waypoint.objects.filter(user=user)] ) from your view while initiating the form pass the user form = waypointForm(user) in case of model form class waypointForm(forms.ModelForm): def __init__(self, … Read more
model._meta.get_all_field_names() will give you all the model’s field names, then you can use model._meta.get_field() to work your way to the verbose name, and getattr(model_instance, ‘field_name’) to get the value from the model. NOTE: model._meta.get_all_field_names() is deprecated in django 1.9. Instead use model._meta.get_fields() to get the model’s fields and field.name to get each field name.
choices = {‘key1′:’val1’, ‘key2′:’val2′} Here’s the template: <ul> {% for key, value in choices.items %} <li>{{key}} – {{value}}</li> {% endfor %} </ul> Basically, .items is a Django keyword that splits a dictionary into a list of (key, value) pairs, much like the Python method .items(). This enables iteration over a dictionary in a Django template.
For Angular 1.0 you should use the $interpolateProvider apis to configure the interpolation symbols: http://docs.angularjs.org/api/ng.$interpolateProvider. Something like this should do the trick: myModule.config(function($interpolateProvider) { $interpolateProvider.startSymbol(‘{[{‘); $interpolateProvider.endSymbol(‘}]}’); }); Keep in mind two things: mixing server-side and client-side templates is rarely a good idea and should be used with caution. The main issues are: maintainability (hard to … Read more
I’ve used a simple technique that works nicely for small cases with no special tags and no additional context. Sometimes this comes in handy {% for i in ‘0123456789’|make_list %} {{ forloop.counter }} {% endfor %}