On Tue, Dec 13, 2011 at 2:21 PM, Jason <1jason.whatf...@gmail.com> wrote:
> Hi there, > > I love the concept of DRY, and django's enthusiasm for this concept. In > light of this I have tried to have a template extend a template, but it > doesn't seem to work. Is there a better way than what I'm currently doing? > > The current way > > index.html > > {% if current_user %} > {% extends "_logged_out_template.html" %} > > {% else %} > {% extends "_logged_in_template.html" %} > {% endif %} > > contact.html > > {% if current_user %} > > {% extends "_logged_out_template.html" %} > > {% else %} > > {% extends "_logged_in_template.html" %} > > {% endif %} > > > > The way I would like it to be (but it didn't work) > > index.html > {% extends "_base_template.html" %} > > _base_script.html > > {% if current_user %} > > {% extends "_logged_out_template.html" %} > > {% else %} > > {% extends "_logged_in_template.html" %} > > {% endif %} > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/django-users/-/8j1xs23yjnAJ. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > Honestly, I never even realized that you *could* put an {% extends %} node inside of an {% if %} node like that. What I find works, if you really do have to dynamically change the base template that you are inheriting from, is to set a context variable in your view, and use it in the template. Nobody ever said that the argument to 'extends' has to be a literal string: View: def my_view(request): if request.user.is_authenticated: base_template = "_logged_in_template.html" else: base_template = "_logged_out_template.html" ... return render("my_template.html", RequestContext({"base_template": base_template...})) Template: (my_template.html) {% extends base_template %} (In a real app, I would use a base class for the view which would set the base template for every request automatically, or I would do it in a context processor, rather than putting those four lines at the top of every single view) -- Regards, Ian Clelland <clell...@gmail.com> -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.