开发者

How to realize a dynamic sidebar with Django?

I plan on creating a sidebar with changing elements (depending on the current url and authentication-status).

For example: The default sidebar shows a login and a tag cloud.

  • If a user is already logged in, I want to display a user menu.
  • If the current url is /tagcloud, I want to hide it from the sidebar.

Actually, I need a way which enables me to do something like this in a view:

def some_view(request):
    if request.user.is_authenticated():
        sidebar.remove('login'开发者_高级运维)
        sidebar.add('user_menu')

def tag_cloud(request):
    sidebar.remove('tag_cloud')

Afterwards, I want to pass the sidebar (implicitly, without passing it to render_to_response) to the template where I have in mind to do something like this:

<div id="sidebar">
    {{ sidebar }}
</div>

Is this possible?


You'd better do this in a context_processors.py file
That also mean you have to use a RequestContext when returning your views

def include_tagcloud(request):
    if request.path == '/tagcould/':
        tagcloud = Tags.objects.filter(active=True) #whatever 
    return {'tagcloud': tagcloud}

def include_login(request):
    if request.user.is_authenticated():
        loginform = MyLoginForm(request.POST) 
        #passing a Django form + POST data in the case of re-submit
    return {'loginform' : loginform}

And then in your template :

{% if loginform %}
      <form action="accounts/login/">
      {{form.as_p}}
      <input type="submit" name="Login">
      </form>
{% endif %}

{% if tagcloud %}
       {%for tag in tagcloud %}.....{%for}
{% endif %}

In this example the login form points to a fixed view,
if you want to catch the login form post on everyview, I don't know how to do

EDIT : if you don't use the CSRF features of Django, you could simply insert the login form in the base template without using any django form and pointing to a login view :

{% if user.is_authenticated %}
    <form action="accounts/login/">
    <input type="text" name="username"><input type="password" name="password">
    <input type="submit" name="Login">
    </form>
{% endif %}


Yeah, but you can use inheritance of templates as well as composition. Then include your sidebar in a parent template that is used/inherited from in all of your templates. Then it is easy to find the template for the sidebar: it's in a separate file.


Answer of @Dominique is correct but When you write something in context_processors that's load at any page of the website. That maybe makes a performance issue.

I think the right way to handle dynamic sidebar is simpletag and use where you need.

def get_sidebar():
    tags = Tags.objects.filter(active=True)
    latest_posts = Post.objects.all().order_by('-create_at')[:10]

    html = render_to_string("sidebar.html", {
        "tags": tags,
        "latest_posts": latest_posts
        })

    return html

And now just use in template files:

<div class="col-md-4 sidebar">
    {% get_sidebar %}
</div>

Also, you can pass request to simpletag to use user.is_authenticated for authenticated user access.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜