缓存 django 模板的部分内容

问题描述 投票:0回答:1

我有一个页面,我只想缓存模板的一部分,因为如果我缓存整个页面,我也会缓存站点标题,但我不需要它。 我尝试在模板中使用标签缓存,但不幸的是请求仍然继续发送到数据库。然而,缓存正在被访问。 可能是什么问题?

lesson.html

{% extends "base.html" %}
{% load static %}

{% block content %}
    <div class="lesson-flex">
        <div class="lesson-flex-nav">
            {% for theme in selected_course.themes.all %}
                <div class="lesson-flex-nav-item">
                    <h1 class="lesson-flex-nav-title">{{ theme.name }}</h1>
                    <ul class="lesson-flex-nav-list">
                        {% for lesson in theme.lessons.all %}
                        <li><a href="{% url 'lesson_detail' course_slug=selected_course.slug lesson_slug=lesson.slug %}" class="lesson-flex-nav-list-item">{{ lesson.name }}</a></li>
                        <div class="sep"></div>
                        {% endfor %}
                    </ul>
                </div>
            {% endfor %}
        </div>
        <div class="lesson-flex-wrapper">
            <div class="lesson-flex-item">
                <h2 class="lesson-flex-item-title">{{ lesson.name }}</h2>
                <div class="sep"></div>
                <p class="lesson-flex-item-text">{{ lesson.body|safe }}</p>
            </div>

            {% if lesson.questions.all %}
                <div class="lesson-flex-wrapper-questions">
                    <h3 class="lesson-flex-wrapper-questions-title">Questions</h3>
                    <div class="sep"></div>
                    {% for question in lesson.questions.all %}
                        <div class="lesson-flex-wrapper-questions-item">
                            <p class="lesson-flex-wrapper-questions-item-title">{{ question.name }}</p>
                            <details>
                                <summary class="lesson-flex-wrapper-questions-item-legend">Answee</summary>
                                <p class="lesson-flex-wrapper-questions-item-answer">{{ question.answer }}</p>
                            </details>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}

            {% if lesson.materials.all %}
                <div class="lesson-flex-wrapper-questions">
                    <h3 class="lesson-flex-wrapper-questions-title">Materials</h3>
                    <div class="sep"></div>
                    {% for material in lesson.materials.all %}
                        <div class="lesson-flex-wrapper-questions-file">
                            {% if material.get_extension_display == '.zip' %}
                                <img class="lesson-flex-wrapper-questions-file-icon" src="{% static 'img/files_extensions_icons/archive_icon.png' %}" alt="ZIP file">
                            {% elif material.get_extension_display == '.jpg' %}
                                <img class="lesson-flex-wrapper-questions-file-icon" src="{% static 'img/files_extensions_icons/jpg_icon.png' %}" alt="JPG file">
                            {% else %}
                                <img class="lesson-flex-wrapper-questions-file-icon" src="{% static 'img/files_extensions_icons/pdf_icon.png' %}" alt="PDF file">
                            {% endif %}
                            <a class="lesson-flex-wrapper-questions-file-link" href="{{ material.file.url }}" download rel="noopener">{{ material.only_filename }}</a>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}
        </div>
    </div>
{% endblock content %}

views.py

class LessonDetailView(DetailView):
     model = Lesson
     template_name = 'courses/lesson.html'
     context_object_name = 'lesson'
     slug_url_kwarg = 'lesson_slug'

     def get_queryset(self):
        queryset = Lesson.objects.filter(slug=self.kwargs['lesson_slug']).prefetch_related('materials', 'questions')

        return queryset

     def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['title'] = self.object.name
        context['selected_course'] = Course.objects.prefetch_related('themes__lessons').get(slug=self.kwargs['course_slug'])

        return context

使用模板标签 - 缓存。

django caching django-views django-templates
1个回答
0
投票

您必须确保使用 {% cache %} 标签正确包装模板的特定部分。

{% extends "base.html" %}
{% load static %}
{% load cache %}  <!-- Make sure to load the cache template tag -->

{% block content %}
    <div class="lesson-flex">
        <div class="lesson-flex-nav">
            {% cache 500 selected_course_themes selected_course.id %}
                {% for theme in selected_course.themes.all %}
                    <div class="lesson-flex-nav-item">
                        <h1 class="lesson-flex-nav-title">{{ theme.name }}</h1>
                        <ul class="lesson-flex-nav-list">
                            {% for lesson in theme.lessons.all %}
                                <li><a href="{% url 'lesson_detail' course_slug=selected_course.slug lesson_slug=lesson.slug %}" class="lesson-flex-nav-list-item">{{ lesson.name }}</a></li>
                                <div class="sep"></div>
                            {% endfor %}
                        </ul>
                    </div>
                {% endfor %}
            {% endcache %}
        </div>

        <div class="lesson-flex-wrapper">
            <div class="lesson-flex-item">
                <h2 class="lesson-flex-item-title">{{ lesson.name }}</h2>
                <div class="sep"></div>
                <p class="lesson-flex-item-text">{{ lesson.body|safe }}</p>
            </div>

            {% if lesson.questions.all %}
                <div class="lesson-flex-wrapper-questions">
                    <h3 class="lesson-flex-wrapper-questions-title">Questions</h3>
                    <div class="sep"></div>
                    {% for question in lesson.questions.all %}
                        <div class="lesson-flex-wrapper-questions-item">
                            <p class="lesson-flex-wrapper-questions-item-title">{{ question.name }}</p>
                            <details>
                                <summary class="lesson-flex-wrapper-questions-item-legend">Answer</summary>
                                <p class="lesson-flex-wrapper-questions-item-answer">{{ question.answer }}</p>
                            </details>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}
            
            {% if lesson.materials.all %}
                <div class="lesson-flex-wrapper-questions">
                    <h3 class="lesson-flex-wrapper-questions-title">Materials</h3>
                    <div class="sep"></div>
                    {% for material in lesson.materials.all %}
                        <div class="lesson-flex-wrapper-questions-file">
                            <a class="lesson-flex-wrapper-questions-file-link" href="{{ material.file.url }}" download rel="noopener">{{ material.only_filename }}</a>
                        </div>
                    {% endfor %}
                </div>
            {% endif %}
        </div>
    </div>
{% endblock content %}

并且必须检查:

{% cache %}
标签需要该部分的唯一缓存键。在上面的示例中,我使用
selected_course_themes
selected_course.id
作为唯一键来缓存特定课程的主题。您可以根据正在渲染的数据以及数据何时更改来调整关键点。

{% cache 500 %} 标签中的数字 500 是缓存持续时间(以秒为单位)(根据需要调整)。

© www.soinside.com 2019 - 2024. All rights reserved.