micgeronimo
micgeronimo

Reputation: 2149

Error with reverse Django

I have following error

Reverse for 'classroom' with arguments '()' and keyword arguments 
'{u'lesson_id': ''}' not found. 1 pattern(s) tried: ['classroom/(?P<lesson_id>\\d+)/$']

models.py:

class DocumentLesson(Lesson):

 document_number = models.ForeignKey('Lesson', related_name='doc_number')
 text = models.TextField(blank=True, null=True)

 def get_absolute_url(self):
    return reverse('classroom', args=[self.id])

urls.py:

url(r'^classroom/(?P<lesson_id>\d+)/$', login_required(classroom), name='classroom'),

views.py:

 def classroom(request, lesson_id=None):
  print 'I am lesson id %s' % lesson_id
  lesson = DocumentLesson.objects.select_related().get(id=lesson_id)
  print ' I am lesson %s' % lesson
  return render(request, 'web/document_lesson.html',{'lesson': mark_safe(lesson.text)})

and template:

<a href="{% url 'classroom' lesson_id=lesson.id %}" ></a>

I can not find problem, prints in classroom work, but error is still thrown. If I make link in template like this

    <a href="{% url 'classroom' lesson_id=3 %}" ></a>

Everything works with no errors. Please advise where is problem here

Upvotes: 0

Views: 56

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

You're passing lesson.text as the lesson variable on the template context. That doesn't have an id field, which is why the error shows an empty string for that value.

Pass the full Lesson object instead and access both text and id in the template.

Upvotes: 2

Related Questions