Django基于类的列表视图的简单分页失败,这要归咎于模板缓存



在我拥有的一个基于网络的社交Django应用程序中,有一个功能允许用户查看过去5分钟内进行会话的所有唯一用户。为此,我使用django user_sessions,一个

使会话对象像其他ORM对象一样成为一级公民。

这些最近的用户显示为模板上的列表。生成的每个名字都可以在这个模板上点击——点击它会将点击者带到被点击者的个人资料页面。为了实现这一点,我简单地将每个用户名封装在这个标签中:<a href="{% url 'profile' slug=unique_session.user.username %}#section0"></a>。该模板被缓存,通过使用模板标签:{%load cache %}{%% cache 30 template_fragment_2 %}{% endcache %}

此外,最近在线用户的列表被分页75(通过在生成所述列表的listview中添加paginate_by = 75来实现)。

问题是:最近用户的列表在模板中加载得很好(点击用户名会得到正确的配置文件),但一旦用户按下"下一页"(,即有超过75人显示),我就会得到错误:django.core.urlresolvers:NoReverseMatch:Reverse for'profile'with arguments'()'and keyword arguments'{u'slug':'}'not found经过大量调试,我仍然不知道为什么会发生这种情况。是因为cachepagination吗?帮助


生成列表的视图如下:

from user_sessions.models import Session
class OnlineView(ListView):
    model = Session
    template_name = "online.html"
    paginate_by = 75
    def get_queryset(self):
        unique_user_sessions = Session.objects.filter(last_activity__gte=(timezone.now()-timedelta(minutes=5))).only('user').distinct('user')
        return unique_user_sessions

模板中的代码是:

{% extends "base.html" %}
{% block content %}
    {% load cache %}
    {% cache 30 template_fragment2 %}
    <div class="margin">
        <ol>
            {% for unique_session in object_list %}
                <li>
                    <a href="{% url 'profile' slug=unique_session.user.username %}#section0">
                        {% if unique_session.user.userprofile.avatar %}
                            <img src="{{ unique_session.user.userprofile.avatar.url }}" alt="no pic" height="20" width="20"></img>
                        {% else %}
                            <img src="{{ STATIC_URL }}img/default-avatar.jpg" alt="no pic" height="20" width="20"></img>
                        {% endif %}
                        {{ unique_session.user.username }}
                    </a>
                </li>
            {% endfor %}
        </ol>
    </div>
    {% endcache %}
{% endblock %}
{% block pagination %}
{% if is_paginated %}
<div class="pagination">
    {% if page_obj.has_previous %}
    <a href="?page={{ page_obj.previous_page_number }}#section0">back</a>
    {% endif %}
    {% if page_obj.has_next %}
    <a href="?page={{ page_obj.next_page_number }}#section0">forward</a>
    {% endif %}
</div>
{% endif %}
{% endblock %}

以下是相关的url模式:

url(r'^users/(?P<slug>[w.@+-]+)/$', UserProfileDetailView.as_view(), name='profile'),
url(r'^online/$', auth(OnlineView.as_view()), name='online'),

最后,

Stack trace
Traceback (most recent call last):
File "/app/.heroku/python/bin/gunicorn", line 11, in <module>
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 74, in run
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 189, in run
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 72, in run
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 174, in run
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 477, in manage_workers
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 540, in spawn_workers
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 124, in init_process
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 119, in run
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 66, in run_for_one
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 30, in accept
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 130, in handle
File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.56.0.42/newrelic/api/web_transaction.py", line 704, in __iter__
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.56.0.42/newrelic/api/web_transaction.py", line 1080, in __call__
File "/app/.heroku/python/lib/python2.7/site-packages/dj_static.py", line 83, in __call__
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.56.0.42/newrelic/api/web_transaction.py", line 1208, in _nr_wsgi_application_wrapper_
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
File "/app/.heroku/python/lib/python2.7/site-packages/django/core/handlers/base.py", line 140, in get_response
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 105, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/response.py", line 82, in rendered_content
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 140, in render
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.56.0.42/newrelic/api/function_trace.py", line 98, in dynamic_wrapper
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 830, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader_tags.py", line 124, in render
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.56.0.42/newrelic/api/function_trace.py", line 98, in dynamic_wrapper
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 830, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
File "/app/.heroku/python/lib/python2.7/site-packages/newrelic-2.56.0.42/newrelic/hooks/framework_django.py", line 702, in wrapper
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/loader_tags.py", line 63, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 830, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
File "/app/.heroku/python/lib/python2.7/site-packages/django/templatetags/cache.py", line 34, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 830, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/base.py", line 844, in render_node
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/defaulttags.py", line 195, in render
File "/app/.heroku/python/lib/python2.7/site-packages/django/template/defaulttags.py", line 424, in render

分页代码本身很好。我认为问题是在结果的第二页上返回的一些user对象有一个空的username,这就是导致NoReverseMatch错误的原因。

最有可能的原因是返回的user对象是一个匿名用户——在这种情况下,username总是一个空字符串。

要验证,请尝试通过将该块包装在此检查中来修改您的模板:

{% if not unique_session.user.is_anonymous %}
    <li>
        <a>...
        </a>
    </li>
{% endif %}

最新更新