Django:NoReverseMatch错误

时间:2015-10-13 07:25:36

标签: python django

在为同学构建一个小型网络应用程序时,我遇到了问题。 获取url变量并将其传递给渲染模板存在问题。有人可以帮忙吗?

回溯:

NoReverseMatch at /board/본과1학년/미생물/
Reverse for 'get_q_table' with arguments '('', '미생물')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['board/(?P<board_grade>\\w+)/(?P<article_subject>\\w+)/q_table/$']
Request Method: GET
Request URL:    http://192.168.56.101:8000/board/%EB%B3%B8%EA%B3%BC1%ED%95%99%EB%85%84/%EB%AF%B8%EC%83%9D%EB%AC%BC/
Django Version: 1.7.6
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'get_q_table' with arguments '('', '미생물')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['board/(?P<board_grade>\\w+)/(?P<article_subject>\\w+)/q_table/$']
Exception Location: /home/web/venv/lib/python3.4/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 468
Python Executable:  /home/web/venv/bin/python

Urls.py(问题到达最后一个网址):

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^index/$', 'exam.views.index', name = 'index'),
    url(r'^board/(?P<board_grade>\w+)/$', 'exam.views.read_board', name = 'board'),
    url(r'^board/(?P<board_grade>\w+)/(?P<article_subject>\w+)/$', 'exam.views.read_article', name = 'read_article'),
    url(r'^board/(?P<board_grade>\w+)/(?P<article_subject>\w+)/q_table/$', 'exam.views.get_q_table', name='get_q_table'),

)

模板渲染期间出错:

In template /home/web/workspace/board/exam/templates/article.html, error at line 26
Reverse for 'get_q_table' with arguments '('', '미생물')' and keyword arguments '{}' not found. 1 pattern(s) tried: ['board/(?P<board_grade>\\w+)/(?P<article_subject>\\w+)/q_table/$']

26                  <form action="{% url 'get_q_table' board.grade article.subject %}" method="POST">
27                      {% csrf_token %}
28                      <div class="form-group">
29                          <label for="professor">출제교수:</label>

型号:

class Board(models.Model):
    user = models.ForeignKey(User)
    grade = models.CharField("학년", max_length=10)
    created_date = models.DateTimeField("생성일", auto_now_add=True)

    def __str__(self):
        return "%s" % (self.grade,)

class Article(models.Model):
    board = models.ForeignKey(Board)
    subject = models.CharField("과목명", max_length=10)
    year = models.PositiveIntegerField("연도", validators=[MinValueValidator(1990), MaxValueValidator(2100)])
    semester = models.PositiveIntegerField("학기", validators=[MinValueValidator(1), MaxValueValidator(2)])
    quarter = models.IntegerField("쿼터", validators=[MinValueValidator(1), MaxValueValidator(3)])
    written_date = models.DateTimeField("작성일", auto_now_add=True)
    updated_date = models.DateTimeField("수정일", auto_now=True)
    #prof_list

    def __str__(self):
        return "%s년 %d학기 %d쿼터: %s" % (self.year, self.semester, self.quarter, self.subject)

Views.py:

def get_q_table(request, board_grade, article_subject):
    article = get_object_or_404(Article, subject=article_subject)

    context = {
        "questions" : Question.objects.all().order_by("number"),
        "article" : article,
    }
    return render(request, "q_table.html", context)

1 个答案:

答案 0 :(得分:0)

{% url 'get_q_table' board.grade article.subject %}的第一个视图参数不存在,因为您没有将board传递给上下文。它被解析为空字符串,与正则表达式(?P<board_grade>\w+)不匹配。

您可能需要使用article.board.grade,或者需要将board变量传递给上下文。