为什么我无法在此 Django 示例应用程序中打印会话变量?

Why I can't print the session variable in this Django example application?

我正在关注与如何构建 Django 应用程序相关的 Mozzilla 文章,发现以下与会话相关的问题,这是具体的文章:https://developer.mozilla.org/it/docs/Learn/Server-side/Django/Sessions

进入我的 Django 门户我有:

  1. 我已经在 locallibrary/locallibrary/settings.py 文件中启用了会话,事实上我已经设置了:

    INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'catalog.apps.CatalogConfig', ]

    中间件 = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ]

那么这是我的 index 查看函数代码定义到 view.py 文件中:

def index(request):
    """View function for home page of site."""

    # Generate counts of some of the main objects
    num_books = Book.objects.all().count()
    num_instances = BookInstance.objects.all().count()

    # Available books (status = 'a')
    num_instances_available = BookInstance.objects.filter(status__exact='a').count()

    # The 'all()' is implied by default.
    num_authors = Author.objects.count()

    # Number of visits to this view, as counted in the session variable.
    num_visits = request.session.get('num_visits', 0)
    request.session['num_visits'] = num_visits + 1

    context = {
        'num_books': num_books,
        'num_instances': num_instances,
        'num_instances_available': num_instances_available,
        'num_authors': num_authors,
    }

    # Render the HTML template index.html with the data in the context variable
    return render(request, 'index.html', context=context)

如您所见,我定义了与会话相关的这些行:

# Number of visits to this view, as counted in the session variable.
num_visits = request.session.get('num_visits', 0)
request.session['num_visits'] = num_visits + 1

所以我将名为 num_visits

的会话变量放入返回的请求中

最后,这是与此视图相关的 HTML 模板(名为 **index.html):

{% extends "base_generic.html" %}

{% block content %}
  <h1>Local Library Home</h1>
  <p>Welcome to LocalLibrary, a website developed by <em>Mozilla Developer Network</em>!</p>

  <h2>UML Models</h2>
  <p>An UML diagram of the site's Django model structure is shown below. </p>
  

  <h2>Dynamic content</h2>
  <p>The library has the following record counts:</p>
  <ul>
    <li><strong>Books:</strong> {{ num_books }}</li>
    <li><strong>Copies:</strong> {{ num_instances }}</li>
    <li><strong>Copies available:</strong> {{ num_instances_available }}</li>
    <li><strong>Authors:</strong> {{ num_authors }}</li>
  </ul>

  <p>You have visited this page {{ num_visits }}{% if num_visits == 1 %} time{% else %} times{% endif %}.</p>
{% endblock %}

如你所见,我添加了这一行:

<p>You have visited this page {{ num_visits }}{% if num_visits == 1 %} time{% else %} times{% endif %}.</p>

应该在我的会话中显示 num_visits 变量值。

问题是渲染的是这样的:

如您所见,它没有显示我的 num_visits 变量的值。但它似乎正在使用该值打印 times 而不是 time

为什么?怎么了?我错过了什么?我该如何解决这个问题?

使用 num_visits 丰富上下文:

    context = {
        'num_books': num_books,
        'num_instances': num_instances,
        'num_instances_available': num_instances_available,
        'num_authors': num_authors,
        'num_visits': num_visits,
    }