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.