2

I'm writing django-based forum, and i've decided it would be suitable for user to browse his last pages. Also, user tracking middleware can be a good aid for suggestions, and so on.

  1. I think, the easiest way to do it is to use the Django Middleware, but i ran into a problem: how to get the title of the page being rendered? Override process_template_response? Can i get the {% block title %} there?

  2. The second server-side way is to use a template tag, i think. In the easiest case, it should should look like {% block title %}{% last_visited _("Page title") %}{% endblock %}.

  3. The third, stupid way: make an ajax script, that will push current user's opened page with title into his session. So, this method will just avoid us getting the page title.

I think, the right way is to get the title block from a template context in middleware. How can i do it?

Thanks.

UPDATE

Made the gist with realisation of the second method using templates and django.cache. The simplest usage:

{% block title %}{% save_visited _("Profile setup") %}{% endblock %}
...
{% load_visited 'visited' %}
{% for title, uri, dt in visited %}
    <a href="{{ uri }}">{{ title }} {% trans "at" %} {{ dt }}</a><br/>
{% endfor %}

Also, i'm still looking for a method allows to get the page's {% block title %} in the middleware. Of course, i can use, i.e., lxml parser and get the title in the process_response method, but it is an ugly overkill.

Thanks for any advice.

4

1 に答える 1

0

あなたの場合、私はわずかに変更された2番目のアプローチを使用します:

{% last_visited  current_page_title  current_page_url  num %}

これは次の 2 つのことを行います。

  1. 現在のタイトルと URL をセッション変数 (最後にアクセスしたページのリスト) に保存します。
  2. 同じセッション変数から最後に訪問した num ページをレンダリングする
于 2013-08-09T11:27:47.440 に答える