Question

i want to add an 'EDIT' link for each posts on index page. but before show this link ; i need to check if session is registered. i mean i need something like this :

{% if session.name=='blabla' %}
    <a href="#">Edit</a>
{% endif %}

i have django.core.context_processors.request on template context processors.

thank you

edit :

here is my detailpage view :

def singlePost(request,postslug):
    post = get_object_or_404(Post, slug=postslug)
    context = {'post':post}
    return render_to_response('detail.html',context,context_instance = RequestContext(request))

when i try this :

def singlePost(request,postslug):
    session=request.session['loggedin']
    post = get_object_or_404(Post, slug=postslug)
    context = {'post':post}
    return render_to_response('detail.html',context,context_instance = RequestContext(request,{'session':'session',}))

it gives template syntax error ( render error)

i tried this :

{% if request.session.name=='blablabla' %}

here is the error :

TemplateSyntaxError at /post/viva-barca

Could not parse the remainder: '=='djangoo'' from 'request.session.name=='djangoo''
Was it helpful?

Solution 2

i found a different way.

{% if post.owner == user %}

        <div class="right"><a href="{% url editpost post.id %}">Edit</a></div>

        {% endif %}

in this way ; i can control user auth too. because there r many users which have their own accounts and posts which are listed in index.html. if i dont write this control ; x user can edit other y user's post. but now only logged in users can edit their own posts.

and if there is no any logged in user ; the 'EDIT' link is not shown.

OTHER TIPS

If you are using django.core.context_processors.request and the template is rendered with a RequestContext then you can access the session off of the request.

{% if request.session.name == 'blabla' %}
    <a href="#">Edit</a>
{% endif %}

Edit:

A RequestContext is used automatically by the render shortcut and the generic views. If you are using render_to_response is needs to be passed using the context_instance argument. This is detailed in the docs for RequestContext https://docs.djangoproject.com/en/1.4/ref/templates/api/#subclassing-context-requestcontext

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top