Cflux
Cflux

Reputation: 1532

NoReverseMatch - reverse for ' ' with keyword arguments 'pk' not found

im pretty sure this is a simple error but I cant seem to figure it out. The error states that the keyword arguments are not found but i have them in the the html.

here is error:

NoReverseMatch at /questions/questionupdate/user2-question3/4/
Reverse for 'detail' with keyword arguments '{'pk': 4}' not found. 1 pattern(s) tried: ['questions/questiondetail/(?P<slug>[\\w-]+)/(?P<pk>\\d+)/$']
Request Method: GET
Request URL:    http://127.0.0.1:8000/questions/questionupdate/user2-question3/4/
Django Version: 1.11
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'detail' with keyword arguments '{'pk': 4}' not found. 1 pattern(s) tried: ['questions/questiondetail/(?P<slug>[\\w-]+)/(?P<pk>\\d+)/$']

file structure:
project: UserTest
--->app:accounts
--->app:questions

models.py

class Question(models.Model):
    class Meta:
        ordering = ['-date_updated']
    user = models.ForeignKey(User, related_name="question")
    # completedTODO: get user working^
    question = models.TextField(blank=False, null=False) # unique=True,
    question_html = models.TextField(blank=False, null=False)
    answer = models.TextField(blank=False, null=False)
    answer_html = models.TextField(blank=False, null=False)
    slug = models.SlugField(unique=True, default='')
    tags = TaggableManager()

    def __str__(self):
        return self.question

    def save(self, *args, **kwargs):
        self.question_html = misaka.html(self.question)
        self.answer_html = misaka.html(self.answer)
        super().save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse(
            "questions:detail",
            kwargs={
                # "slug": self.slug,
                "pk": self.pk
            }
        )

urls.py i am trying to pass both slug and pk to url

url(r'questiondetail/(?P<slug>[\w-]+)/(?P<pk>\d+)/$', views.QuestionDetail.as_view(), name='detail'),
url(r'questionupdate/(?P<slug>[\w-]+)/(?P<pk>\d+)/$', views.QuestionUpdate.as_view(), name='update'),

views.py

class QuestionDetail(generic.DetailView):
    model = models.Question

class QuestionUpdate(generic.UpdateView):
    model = models.Question
    form_class = QuestionForm
    template_name = "questions/question_form_update.html"

question_detail.html

<h3><a href="#">{{ question.user }}</a></h3>
<h3>{{ question.question_html|safe }}</h3>
<h3>{{ question.answer_html|safe }}</h3>
<a href="{% url 'questions:update' slug=question.slug pk=question.pk %}">Update Question</a>

Any help is appreciated!

Upvotes: 0

Views: 5888

Answers (1)

hidalgo
hidalgo

Reputation: 156

It seems that the issue is caused by commented line ;)

def get_absolute_url(self):
        return reverse(
            "questions:detail",
            kwargs={
                # "slug": self.slug,
                "pk": self.pk
            }
        )

Uncomment it and it should work.

Upvotes: 3

Related Questions