Django: href {% url %} issue
15,411
This is correct. If not - your urls.py
seems to be wrong. Please post it.
<a href="{% url answers.views.display_answers question.id %}">View answers</a>
Edit Here's a better version of your view.
from django.template import RequestContext
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response, redirect, get_object_or_404
def display_answers(request, q_id):
q = get_object_or_404(Question, id=q_id)
ans_list = Answer.objects.filter(question=q)
context = {'question': q, 'ans_list': ans_list}
return render_to_response('view_answers.html', context, RequestContext(request))
Author by
Nicholas Teo
Updated on June 04, 2022Comments
-
Nicholas Teo almost 2 years
Why does
<a href="{% url 'answers.views.display_answers' Question.id %}">View answers</a>
in my template translate to this interpretation by Django:
Request URL: http://127.0.0.1:8000/questions/%7B%%20url%20'answers.views.display_answers'%20Question.id
which of course leads to an url mismatch error.
Seems like its reading in my '{' in ASCII form. Can anyone enlighten me as to why it is so?
EDIT:
This was how i rendered the template--
return render(request, 'display_questions.html', context)
and the template contains the href. My display answer view redirects to another view as such:
def display_answers(request, q_id): q = get_object_or_404(Question, id=q_id) ans_list = Answer.objects.filter(question=q) context = {'question': q, 'ans_list': ans_list} return redirect('view_answers.html', context)
Error:
The current URL, questions/{% url 'answers.views.display_answers' Question.id, didn't match any of these.
-
Thomas Schwärzl over 11 yearsdoesn't
redirect
need thereverse
for named views? likereturn redirect(reverse('your_view_answers_view'))
? -
dgel over 11 yearsDidn't mean to downvote you, but now it's locked in. Can you edit your post insignificantly so it will allow me to change my vote?
-
Thomas Schwärzl over 11 yearsit doesn't matter - i'm not that "omg i need more points here!!1" guys :) but thx for being honest