1

I've stuck on a problem while building a little web app for my classmates. There's problem getting url variables and passing on those to render templates. Can anyone help?

Traceback:

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 (Problem reaching the last url):

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'),

)

Error during template rendering:

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>

Models:

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)
Bossam
  • 744
  • 2
  • 9
  • 24
  • 3
    Possible duplicate of [how to have unicode characters in django url?](http://stackoverflow.com/questions/20514455/how-to-have-unicode-characters-in-django-url) – ojii Oct 13 '15 at 07:30

1 Answers1

0

The first view argument to {% url 'get_q_table' board.grade article.subject %} does not exist, since you're not passing in board to the context. It is resolved to an empty string, which does not match the regex (?P<board_grade>\w+).

You probably need to use article.board.grade instead, or you need to pass in a board variable to the context.

knbk
  • 52,111
  • 9
  • 124
  • 122
  • sorry i don't quite follow you. What do you mean by 'first argument'? How should I NOT make it empty? – Bossam Oct 13 '15 at 10:27
  • @Bossam The first view argument in your `{% url %}` tag. I've updated my answer, it's empty because `board.grade` does not exist in your template context. – knbk Oct 13 '15 at 11:17
  • I solved the problem. It worked after I added 'primary_key=True' attribute to Board class 'grade' field. I guess the variables extracted from urls.py must be set as primary_key right? Do you by any chance know why? – Bossam Oct 14 '15 at 03:30