开发者

Django messaging & template inheritance - how to use it?

First of all: Hello StackOverflow folks :) . That's my first post here. Okay, on to the thing: The Django messaging framework does not work in my little application. I have the following layout:

  • base.html -> index.html (inherits from base.html)
  • an importcsv view (which does nothing yet, but wants to go back to index.html and display a message there, like "import successful")
  • a basically vanilla django 1.3, os x
  • the template inheritance works quite nicely

And however I tried, I cannot get messaging to work. I basically copied-and-pasted the examples from the Django docs, but to no avail.

On to the code.

base.html (shortened to the essentials):

<html>
<head>
    {% block e开发者_如何学Goxtra-css %}
    {% endblock %}
</head>
<body>
{% if messages %}
<ul class="messages">
    {% for message in messages %}
    <li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
    {% endfor %}
</ul>
{% endif %}
</body>
</html>

index.html:

{% extends "base.html" %}

views.py: (rtr is simply "import render_to_response as rtr")

def _importlegs(request):
    f = request.FILES['file']
    print "_importlegs"
    messages.info(request, "Test")
    return rtr('index.html', RequestContext(request))

The result is, that the "messages" block is never, ever displayed. No matter what I do.


Make sure that the messages middleware is in your MIDDLEWARE_CLASSES and that django.contrib.messages is in your INSTALLED_APPS. If it wasn't, do a syncdb afterwards so that the messages tables get created in your databse.

Also, try making {} the second argument to rtr so that RequestContext(request) is the third.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜