Passage par les pk partout.

This commit is contained in:
Hugo LEVY-FALK 2018-01-30 23:07:08 +01:00
parent 0dafa938e8
commit 5869ec943c
4 changed files with 8 additions and 8 deletions

View file

@ -10,7 +10,7 @@ class Category(models.Model):
verbose_name="Nom de la catégorie" verbose_name="Nom de la catégorie"
) )
def get_absolute_url(self): def get_absolute_url(self):
return reverse('content:category-list', kwargs={'category_id':self.id}) return reverse('content:category-list', kwargs={'pk':self.pk})
def __str__(self): def __str__(self):
return self.name return self.name

View file

@ -10,7 +10,7 @@ from .views import (
app_name = 'content' app_name = 'content'
urlpatterns = [ urlpatterns = [
path( path(
'category/<int:category_id>/', 'category/<int:pk>/',
ContentCategoryList.as_view(), ContentCategoryList.as_view(),
name='category-list' name='category-list'
), ),

View file

@ -12,14 +12,14 @@ class ContentCategoryList(generic.ListView):
template_name = "content/content_list.html" template_name = "content/content_list.html"
def get_queryset(self): def get_queryset(self):
category_id = self.kwargs['category_id'] pk = self.kwargs['pk']
category = get_object_or_404(Category, id=category_id) category = get_object_or_404(Category, pk=pk)
return Content.objects.filter(category=category) return Content.objects.filter(category=category)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super(generic.ListView, self).get_context_data(**kwargs) context = super(generic.ListView, self).get_context_data(**kwargs)
category_id = self.kwargs['category_id'] pk = self.kwargs['pk']
category = get_object_or_404(Category, id=category_id) category = get_object_or_404(Category, pk=pk)
context['category'] = category context['category'] = category
return context return context

View file

@ -17,12 +17,12 @@
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav"> <ul class="nav navbar-nav">
{% for c in categories %} {% for c in categories %}
{% if category.id == c.id %} {% if category.pk == c.pk %}
<li class="active"> <li class="active">
{%else%} {%else%}
<li> <li>
{%endif%} {%endif%}
<a href="{% url 'content:category-list' category_id=c.id %}">{{c.name}} <a href="{% url 'content:category-list' c.pk %}">{{c.name}}
</a></li> </a></li>
{% endfor %} {% endfor %}
<li><a href="{% url 'vote:home' %}">Vote</a></li> <li><a href="{% url 'vote:home' %}">Vote</a></li>