Angularjs with Django - Conflicting Template Tags

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) and security (double interpolation could expose a new security vector - e.g. while escaping of serverside and clientside templating by themselves might be secure, their combination might not be).
  • if you start using third-party directives (components) that use {{ }} in their templates then your configuration will break them. (fix pending)

While there is nothing we can do about the first issue, except for warning people, we do need to address the second issue.

how to use angular expression inside template's tag of django?

You can't. You have to think about, a workaround. From Django 1.5 you have the {% verbatim %} tag that prevents anything in it from rendering (including {{}} signs).

{% getUserName request {[{ angular_conversation.user_id }]} %} for what you are using this for? Can't you do it like {{ request.user.username }}?

Javascript client-side framework not conflict with django template language

angularJS works fine with django template language with one simple change.

The $interpolateProvider service controls the interpolation symbols, which can be changed, angularjs use of {{}} is just a default.

http://docs.angularjs.org/api/ng.$interpolateProvider 

To change the symbols, to say '{[{}]}', its simply:

 app.config(function($interpolateProvider) {
$interpolateProvider.startSymbol('{[{');
$interpolateProvider.endSymbol('}]}');
});

Django query with conflicting fields returns duplicate answers

Use a single filter condition and use .distinct(…) [Django-doc] to return distinct DwCloudSystems:

from django.db.models import Q

DWCloudSystem.objects.filter(
Q(isShared=False, company_key=comp) |
Q(isShared=True, location__owners=comp)
).distinct()

Using a angularjs variable in a django template

You can't do that. Template tags are server side while angularjs is rendered in client side. The value of the angularjs variable won't be available until the client's browser renders it.

If you have to do that, this variable should be calculated in the Django view and not in angularjs.

Django Template, how to parse items with symbols

You can get the value of dictionary with the help of django template tags.

Template tag example

@register.filter
def get_attr(obj, attr):
return obj.get(attr)

As you mentioned context is dict therefore I used obj.get but you can use any other methods depending on obj.

Template usage

{{ result|get_attr:'@url' }}

I defined result in views.py as

context = {
'result' : {'@url':'a123'}
}


Related Topics



Leave a reply



Submit