Monday, December 1, 2014

Re: template url reverse and namespacing is driving me crazy

This, this is not about improving the documentation. It's pretty clear how to solve the problem.

The issue here is whom should have the responsibility of setting the correct current_app in the template context. One can always fix 3rd party apps but it gets old after a while. I for one can't name a single reusable app that correctly sets the current_app in it's views - which leads me to believe this is a widespread issue and Django could have better defaults.

A provisory solution would be having a context processor like this:

  def add_default_namespace(request):
    return {'current_app': request.resolver_match.namespace}

On Monday, December 1, 2014 4:33:40 PM UTC+2, Tim Graham wrote:
Did you read about passing 'current_app' to your template rendering calls? https://docs.djangoproject.com/en/1.7/topics/http/urls/#id4

The contrib.admin class has app_name='admin' and namespace='admin' by default, but you can create a custom AdminSite(name='foo') to customize the instance namespace. In the admin's views, calls to reverse and template rendering pass (current_app=self.admin_site.name). That should be a good example to follow in making your app reusable.

If you think the documentation could be improved, I'd happily review any patch you could submit, thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d91f1aac-8e46-4a3d-9deb-5e626323848e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment