python - Why do my user permissions disappear when I switch between views? -


i've created hackernews clone. users given permissions vote, submit story etc. each user, when logged in has session id, i've tested , believe works fine.

however, when switch between views (from localhost/ localhost/science example), permissions seem disappear.

here part of views.py:

def index(request, category_id=1):     stories = top_stories(top=30)     category = category.objects.get(id = category_id)     if request.user.is_authenticated():         liked_stories = request.user.liked_stories.filter(id__in = [story.id story in stories])     else:          liked_stories = []     return render(request, 'stories/index.html', {         'stories': stories,         'user': request.user,         'liked_stories': liked_stories,         'category': category,         }) 

and here category function sorts stories of particular category:

def category(request, category_name):     template = 'stories/category.html'     category = category.objects.get(category_name = category_name)     return render_to_response(template, {         'category': category         }) 

when go localhost/{{category}} link, permissions such voting , submitting story seem disappear.

here base.html:

 <body>         <div id = "content">             <div id = "header">                 {% if user.is_authenticated %}                     <div id = "user-info">{{user.username }} | <a href = "/story/">sumbit</a> | <a href = "/logout/">logout</a></div>                  {% else %}                      <div id = "user-info"><a href = "/login/">login</a></div>                     <div id = "user-info"><a href = "/register/">sign up</a></div>                  {% endif %}                 <div id = "site-title"><h1><a href = "/">newsfeed</a></h1></div>             </div>             {% block content %}             {% endblock content %}         </div>     </body> 

index.html:

<ol> {% story in stories %}     <li>         <p class = "story-title">         {% if user.is_authenticated , story not in liked_stories %}             <a href = "/vote/" id = "story-vote-{{ story.id }}" class = "vote"><img src = "static/images/arrow.png" height = "20px" width = "20px"></a>             <a href = "{{ story.url }}" id = "story-title-{{ story.id }}" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>         {% else %}             <a href = "{{ story.url }}" style = "margin-left: 15px;" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>         {% endif %}         </p>          <p class = "story-info">             {{story.points}} points {{ story.moderator.username }} {{story.created_at|age}} | <a href = "{{ story.category.category_name }}/{{ story.id }}/"> comment </a>             | <a href = "/{{ story.category.category_name }}/">{{ story.category }} </a>         </p>     </li>   {% endfor %} </ol> 

and category.html:

<ol> {% story in category.story_set.all %}     <li>         <p class = "story-title">         {% if user.is_authenticated , story not in liked_stories %}             <a href = "/vote/" id = "story-vote-{{ story.id }}" class = "vote"><img src = "static/images/arrow.png" height = "20px" width = "20px"></a>             <a href = "{{ story.url }}" id = "story-title-{{ story.id }}" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>         {% else %}             <a href = "{{ story.url }}" style = "margin-left: 15px;" target="_blank">{{story.title}} </a> <span class = "domain"> ({{ story.domain}}) </span>         {% endif %}         </p>          <p class = "story-info">             {{story.points}} points {{ story.moderator.username }} {{story.created_at|age}} | <a href = "{{ story.id }}/"> comment </a>             | <a href = "/{{ story.category.category_name }}/">{{ story.category }} </a>         </p>     </li> {% endfor %} </ol> 

is there i'm missing rectify issue? also, if need other files let me know in comments. luca

the index view works because including user in template context. if have auth context processor enabled (it default), don't need include user in template context, because using render shortcut.

return render(request, 'stories/index.html', {     'stories': stories,     'liked_stories': liked_stories,     'category': category,     }) 

the category view not working because using render_to_response, , have not explicitly included user in template context.

you can make work render_to_response including context_instance (see the docs), easiest fix use render shortcut category view well.

return render(request, template, {     'category': category     }) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -