1
You can specify a default value in your blocks, such as:
[...]
<title>{% block page_title_header %}Page title{% endblock %}</title>
[...]
<h1>{% block page_title_body %}Page title{% endblock %}</h1>
When you omit these blocks in your normal templates, default values will be used.
1
Try:
<title>{% block page_title_header %}
{% if somevar_title %}
{{ somevar_title }}
{% else %}
Default title
{% endif %}
{% endblock %}</title>
And put somevar_title in your context. In your view:
context = {'somevar_title': Page.objects.get(pk=something).title}
return render(request, self.template_name, context)
You just have to remember to add title to your context on every view that is going to use the template.
You could also update the context with class based views:
class MyClass(TemplateView):
def get_context_data(self, **kwargs):
context = super(MyClass, self).get_context_data(**kwargs)
#get your title here...whatever you need it to be
context['somevar_title'] = Something_to_get_my_title
return context
Then, if you are getting your title from a slug or some way that can be resolved in a generic way just subclass MyClass and you will have the variable in the context. For example, using something like Page.objects.get(pk=kwargs.get('page_id'))
could do the trick (don’t forget to add logic to handle non valid id’s).
- [Answered ]-Django which is the correct way to store discussion inside db?
- [Answered ]-MultiValueDictKeyError: "'password'"
0
Use block.super. Example:
{% block page_title %}
{% with "foo" as title %}
{{ block.super }}
{% endwith %}
{% endblock %}
- [Answered ]-Multiply Django Apache servers
- [Answered ]-Show the custom message in Django form
- [Answered ]-Custom User profile for Django user
- [Answered ]-Django URL to View mapping
Source:stackexchange.com