Can I access constants in settings.py from templates in Django?

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

How to access a dictionary element in a Django template?

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.

Creating a dynamic choice field

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

Iterate over model instance field names and values in template

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.

AngularJS with Django – Conflicting template tags

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