简体   繁体   English

反向Django错误

[英]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: 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: urls.py:

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

views.py: 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. 您正在将lesson.text作为模板上下文上的lesson变量传递。 That doesn't have an id field, which is why the error shows an empty string for that value. 那没有id字段,这就是为什么错误显示该值的空字符串的原因。

Pass the full Lesson object instead and access both text and id in the template. 而是传递完整的Lesson对象,并访问模板中的文本和id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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