Django template inheritance and context

I read the final tutorial on django and chapter 4 on template inheritance. It seems that I am not doing something as elegant as it should be possible, since I have to duplicate some code so that the context is displayed when the child view is called. Here is the code in views.py:

def homepage(request):
    current_date = datetime.datetime.now()
    current_section = 'Temporary Home Page'
    return render_to_response("base.html", locals())
def contact(request):
    current_date = datetime.datetime.now()
    current_section = 'Contact page'
    return render_to_response("contact.html", locals())

It seems redundant to include the current_date string in each function.

Here is the basic html file that the main page calls:

<html lang= "en">
<head>
    <title>{% block title %}Home Page{% endblock %}</title>
</head>
<body>
    <h1>The Site</h1>
    {% block content %}
        <p> The Current section is {{ current_section }}.</p>
    {% endblock %}

    {% block footer %}
    <p>The current time is {{ current_date }}</p>
    {% endblock %}
</body>
</html>

and child template file:

{% extends "base.html" %}

{% block title %}Contact{% endblock %}

{% block content %}
<p>Contact information goes here...</p>
    <p>You are in the section {{ current_section }}</p>
{% endblock %}

If I do not include the current_date line when calling the child file where this variable should be displayed, this is empty.

+5
source share
3 answers

, :

1.

settings.py:

# settings.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'myapp.context_processors.default', # add this line
    'django.core.context_processors.auth', 
)

, context_processors.py . , default ( , settings.py), . , .

2.

# context_processors.py

from datetime import datetime
from django.conf import settings  # this is a good example of extra
                                  # context you might need across templates
def default(request):
    # you can declare any variable that you would like and pass 
    # them as a dictionary to be added to each template context:
    return dict(
        example = "This is an example string.",
        current_date = datetime.now(),                
        MEDIA_URL = settings.MEDIA_URL, # just for the sake of example
    )

3.

- , RequestContext() . views.py, :

# old views.py
def homepage(request):
    current_date = datetime.datetime.now()
    current_section = 'Temporary Home Page'
    return render_to_response("base.html", locals())

def contact(request):
    current_date = datetime.datetime.now()
    current_section = 'Contact page'
    return render_to_response("contact.html", locals())


# new views.py
from django.template import RequestContext

def homepage(request):
    current_section = 'Temporary Home Page'
    return render_to_response("base.html", locals(),
                              context_instance=RequestContext(request))

def contact(request):
    current_section = 'Contact page'
    return render_to_response("contact.html", locals(),
                              context_instance=RequestContext(request))
+16

, django.views, generic.simple.direct_to_template render_to_response. RequestContext .

from django.views,generic.simple import direct_to_template

def homepage(request):
    return direct_to_template(request,"base.html",{
        'current_section':'Temporary Home Page'
    })

def contact(request):
    return direct_to_template(request,"contact.html",{
        'current_section':'Contact Page'
    })

urls.py,

urlpatterns = patterns('django.views.generic.simple',
    (r'^/home/$','direct_to_template',{
        'template':'base.html'
        'extra_context':{'current_section':'Temporary Home Page'},        
    }),
    (r'^/contact/$','direct_to_template',{
        'template':'contact.html'
        'extra_context':{'current_section':'Contact page'},        
    }),
+3

django v1.8 + , , .

1. TEMPLATES settings.py

TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': [],
       'APP_DIRS': True,
       'OPTIONS': {
           'context_processors': [
               'django.template.context_processors.debug',
               'django.template.context_processors.request',
               'django.contrib.auth.context_processors.auth',
               'django.contrib.messages.context_processors.messages',

               'your_app.context_processor_file.func_name',  # add this line

           ],
       },
   },
]

2.

context_processor_file.py

def func_name(request):
  test_var = "hi, this is a variable from context processor"
  return {
    "var_for_template" : test_var,
  }

3. var_for_template

, : base.html

<h1>{{ var_for_template }}</h1>  

:

<h1>hi, this is a variable from context processor</h1>
Hide result

django 1.8+ django doc

0

All Articles