简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM