Django: Trouble with named URLs instead of ids
<p>So I'm making a study app that involves flash cards. A user can make subjects and put decks containing cards in them. So for example, in the Biology subject, there would be a Deck called "unit one" and in that deck would be full of cards. The URL for a deck would ideally look like</p>

<pre><code>localhost:8000/subjects/Biology/unit-one/
</code></pre>

<p>Here is my code</p>

<p>views.py</p>

<pre><code>class IndexView(generic.ListView):
  template_name = 'card/index.html'
  context_object_name = 'subjects'

  def get_queryset(self):
      return Subject.objects.all()

class SubjectView(DetailView):
  model = Subject
  slug_field = "subject"
  template_name = 'card/subject.html'

class DeckView(DetailView):
  model = Deck
  template_name = 'card/deck.html'

  def get_object(self, subjects, deck):
      subject_obj = Subject.objects.filter(subject_name=subjects).first()
      obj = Deck.objects.filter(subject=subject_obj, deck_name=deck).first()
      return obj

  def get(self, request, subjects, deck):
      self.object = self.get_object(subjects, deck)
      context = self.get_context_data(object=self.object)
      return self.render_to_response(context)
</code></pre>

<p>models.py</p>

<pre><code>class Subject(models.Model):
  subject_name = models.CharField(max_length=100)
  description = models.TextField()

  def __str__(self):
      return self.subject_name

  def get_absolute_url(self):
      return reverse('card:index')

class Deck(models.Model):
  deck_name = models.CharField(max_length=100)
  subject = models.ForeignKey(Subject, on_delete=models.CASCADE)

  def __str__(self):
      return self.deck_name

class Card(models.Model):
  term = models.CharField(max_length=100)
  definition = models.TextField()
  deck = models.ForeignKey(Deck, on_delete=models.CASCADE)

  def __str__(self):
      return self.term
</code></pre>

<p>urls.py</p>

<pre><code>url(r'^subjects/(?P&lt;subject&gt;[\w ]+)/$', views.SubjectView.as_view(), name='subject'),
url(r'^subjects/(?P&lt;subject&gt;[\w ]+)/(?P&lt;deck&gt;[\w ]+)/$', views.DeckView.as_view(), name='deck'),
</code></pre>

<p>index.html</p>

<pre><code>&lt;li&gt;&lt;a href="{% url 'card:subject' subject.subject_name %}"&gt;
</code></pre>

<p>subject.html</p>

<pre><code>&lt;ul&gt;
  {% for deck in subject.deck_set.all %}
  &lt;li&gt;&lt;a href="{% url 'card:deck' deck.deck_name %}"&gt;{{deck.deck_name}}&lt;/a&gt;&lt;/li&gt;
  {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>However, I am getting this error.</p>

<pre><code>Generic detail view SubjectView must be called with either an object pk or a slug.
</code></pre>

<p>The index page is showing properly, but when I click on a subject, I get that error. How do I fix this?</p>

<p>Have you tried setting <a href="https://docs.djangoproject.com/en/1.10/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.slug_url_kwarg" rel="nofollow"><code>SubjectView.slug_url_kwarg</code></a>?</p>

python django