django kwargs are not passed with the view to html template

177 views
Skip to first unread message

Mohammed Modhaffer

unread,
Feb 4, 2019, 7:24:55 AM2/4/19
to Django users
I searched for hours but I could not find a solution. I am trying to filter conferences by country. Conference detail url works fine when I click on it from the main conference list. When I filter by country, the list of conferences in a given country are shown. However, when I click on a conference, it breaks and shows `Reverse for 'conference-detail' with keyword arguments '{'pk': '', 'slug': ''}' not found. 1 pattern(s) tried: ['conferences\\/details\\/(?P<pk>[0-9]+)\\/(?P<slug>[-a-zA-Z0-9_]+)\\/$']`. I cannot figure out the problem in my code.

Model:
 
    from cities_light.models import Country, City
    class Conference(models.Model):
        title = models.CharField(max_length=512, default='')
        slug = models.SlugField(max_length=512)
        country = models.ForeignKey(Country, on_delete=models.CASCADE)
        city = models.ForeignKey(City, on_delete=models.CASCADE)
       
        def __str__(self):
            return self.title

        def save(self, *args, **kwargs):
            self.slug = slugify(self.title)
            super(Conference, self).save(*args, **kwargs)

        def get_absolute_url(self):
            return reverse('conferences:conference-detail', kwargs={'pk': self.pk, 'slug': self.slug})

urls.py:

 

    path('', views.ConferenceListView.as_view(), name='conference-list'),
    path('details/<int:pk>/<slug:slug>/', views.ConferenceDetailView.as_view(), name='conference-detail'),
    path('country-list', views.ConferenceCountryListView.as_view(),
                                                 name='conference-country-list'),
    path('country/<int:pk>/<slug:slug>/', views.ConferenceByCountryView.as_view(),
                                                 name='conferences-in-single-country'),

Views:

    class ConferenceByCountryView(generic.ListView):
        template_name = 'conferences/conferences_in_country_list.html'
        context_object_name = 'conferences'

        def get_queryset(self, *args, **kwargs):
            country = Country.objects.get(pk=self.kwargs.get('pk', None))
            return Conference.objects.filter(country=country)

        def get_context_data(self, **kwargs):
            context = super(ConferenceByCountryView, self).get_context_data(**kwargs)
            country = Country.objects.get(pk=self.kwargs.get('pk', None))
            context['country'] = country
            context['title'] = 'Conferences in ' + ' country'
            return context


    class ConferenceDetailView(generic.DeleteView):
        model = Conference
        template_name = 'conferences/conference_detail.html'

        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context['title'] = self.object.title
            context['pk'] = self.object.pk
            context['slug'] = self.object.slug
            return context

In the conference list html, this is the code snippent: `<a href="{% url 'conferences:conference-detail' pk=conference.pk slug=conference.slug %}"><h6>{{ conference.title }}</h6></a>`. It redirects to the conference detail page without any problem. In the conference by country html, I have the following code:

    <ul class="list-group">
                {% for conferenc in conferences %}
                    <a class="list-group-item list-group-item-action"  href="{% url 'conferences:conference-detail' pk=conference.pk slug=conference.slug %}"> {{ conferenc.title }} </a> {%comment%} Here is the problem. {% endcomment %}
                {% endfor %}
                 </ul>

When I click on the url, it raises the following error:

    Reverse for 'conference-detail' with keyword arguments '{'pk': '', 'slug': ''}' not found. 1 pattern(s) tried: ['conferences\\/details\\/(?P<pk>[0-9]+)\\/(?P<slug>[-a-zA-Z0-9_]+)\\/$']

Your kind help is highly appreciated.

Karen Tracey

unread,
Feb 4, 2019, 7:32:26 AM2/4/19
to django...@googlegroups.com

In the conference list html, this is the code snippent: `<a href="{% url 'conferences:conference-detail' pk=conference.pk slug=conference.slug %}"><h6>{{ conference.title }}</h6></a>`. It redirects to the conference detail page without any problem. In the conference by country html, I have the following code:

    <ul class="list-group">
                {% for conferenc in conferences %}
                    <a class="list-group-item list-group-item-action"  href="{% url 'conferences:conference-detail' pk=conference.pk slug=conference.slug %}"> {{ conferenc.title }} </a> {%comment%} Here is the problem. {% endcomment %}
                {% endfor %}
                 </ul>

This for loop has variable named conferenc but in the urtl tag the name conference is used. The same name needs to be used in both places. (If you change to conference note the use of conferenc to show the title will also need to change. 

(Thanks for all the detail in the question!)

Mohammed Modhaffer

unread,
Feb 4, 2019, 10:28:37 AM2/4/19
to django...@googlegroups.com, Karen Tracey
Thank you Karen,
Just now I realized that it was a typo which caused this pain. From now on, I should type slowly.

Best regards!
Mohammed.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACS9raciD2hFjbhuQjqBTTnydpQsNe_Ph3CmdehDzbXpvn2B5A%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages