Using templates

  • First, add the template direction in setting.py/TEMPLATES
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        # modify the DIRS as below:
        'DIRS': [
            os.path.join(BASE_DIR, 'templates'),
        ],
        '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',
            ],
        },
    },
]
  • call the template in the views.py
def blog_detail(request, blog_pk):
    context = {'blog': get_object_or_404(Blog, pk=blog_pk)}
    return render_to_response('blog/blog_detail.html', context)
  • template files often be stored into folder base_dir/template
  • in a base template file:
  • we can define the extend part in a base template file, using {% block %} ... {% endblock %}

   
   {% block title %}{% endblock %}
   
   {% block header_extends %}{% endblock %}

  • we can call the base template file in another template file:
{% extends 'base.html' %}

{# Title of the page #}
{% block title %}
    {{ blog_type.type_name }}
{% endblock %}

{% block content %}
    

Current Category: {{ blog_type.type_name }}

{% for blog in blogs %}

{{ blog.title }}

{{ blog.content|truncatechars:30 }}

{% empty %} {% endfor %} {% endblock %}

你可能感兴趣的:(Using templates)