python - Extending a template thats already extended in Django -


i'm trying figure out if there way extend partial view view extends base.html.

here example of i'm trying do:

my-template.html

{% extends 'base.html '%}  <div class="row">   <div class="col-xs-12">     <ul class="nav nav-tabs">       <li role="presentation" class="active"><a href="#">tab1</a></li>       <li role="presentation"><a href="#">tab2</a></li>     </ul>   </div> </div>  <div> {% block tab_content %} {% endblock %} </div> 

partial1.html

{% extends 'my-template.html' %}  {% block tab_content %} <h1>i'm partial 1</h1> {% endblock %} 

the my-template.html view has url constructed so:

url(r'^my-template/(?p<id>[0-9]+)/$', views.my_template_view, name='my-template') 

in addition context dict passed my_template_view providing id url.

i user click on tab , corresponding partial rendered url so:

url(r'^my-template/(?p<id>[0-9]+)/tab1/$', views.tab1_view, name='tab1-view') 

but right i'm getting noreversematch @ /my-template/97/tab1/ i'm assuming means tab1_view doesn't have access same context my_template_view , can't id build reverse of url.

in template /partial1.html, error @ line 0 reverse 'tab1_view' arguments '('',)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['/my-template/(?p<id>[0-9]+)/tab1/$'] 

so, there way me, @ least, pass along context or id works, or going in entirely wrong way?

the typical way solve using include template tag, not extending new template.

here django doc describing this.

you can use variable define dynamic template name included based on logic in view.

little more clarification here:

you can have url route direct same view , have "tab" optionally passed in second parameter so:

url(r'^my-template/(?p<id>[0-9]+)/(?p<tab_name>\w+)/$', views.my_template_view, name='my-template') url(r'^my-template/(?p<id>[0-9]+)/$', views.my_template_view, name='my-template') 

and view like:

def my_template_view(request, id, tab_name=none):     if not tab_name:         tab_name = "tab1"     if tab_name == "tab1":         partial = "tab1.django.html"     elif tab_name == "tab2":         partial = "tab2.django.html"     return render("my-template.html", { 'partial': partial }) 

and on template have:

{% include partial %} 

because included template have same context, have access variables available in original context well.


Comments