Removing Hardcoded urls in Templates

114 views
Skip to first unread message

Bruckner de Villiers

unread,
Dec 10, 2019, 5:58:29 AM12/10/19
to django...@googlegroups.com

Running Django 3.0 & Python 3.7.3.

 

Issue with Tutorial Part 3:

I replaced “<li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>” with
“<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>” and get the following error:
Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name.

<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>

index.html:

{% if latest_question_list %}

    <ul>

    {% for question in latest_question_list %}

        {% comment %} <li><a href="/polls/{{ question.id }}/">{{ question.id }} {{ question.question_text }} {{ question.pub_date }}</a></li> {% endcomment %}

        <li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li> {% comment %} - Why doesn't this work?-->  {% endcomment %}

    {% endfor %}

    </ul>

{% else %}

    <p>No polls are available.</p>

{% endif %}

urls.py:

from os import \

    path

 

from django.urls import \

    path

from . import \

    views

 

app_name = 'polls'

urlpatterns = [

    path('', views.index, name='index'),

    path('<int:question_id>/', views.detail, name='detail'),

    path('<int:question_id>/results/', views.results, name='results'),

    path('<int:question_id>/vote/', views.vote, name='vote'),

]

views.py:

from django.shortcuts import get_object_or_404, render

from django.http import \

    HttpResponse

from django.shortcuts import \

    render

#from django.template import loader

from .models import Question

 

# Create your views here.

def index(request):

    latest_question_list = Question.objects.order_by('-pub_date')[:10]

    context = {'latest_question_list': latest_question_list}

    return render(request, 'polls/index.html', context)

 

def detail(request, question_id):

    question = get_object_or_404(Question, pk=question_id)

    return render(request, 'polls/detail.html', {'question': question})

 

def results(request, question_id):

    response = "You're looking at the results of question %s."

    return HttpResponse(response % question_id)

 

def vote(request, question_id):

    return HttpResponse("You're voting on question %s." % question_id)

 

Much obliged,
 

 

 

Bruckner de Villiers

083 625 1086

Sencer Hamarat

unread,
Dec 10, 2019, 6:51:08 AM12/10/19
to django...@googlegroups.com
Would you please replace url name with 'polls:detail', as described at https://docs.djangoproject.com/en/3.0/topics/http/urls/#id5


Saygılarımla,
Sencer HAMARAT



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0072F811-155B-41C2-BDCD-3C529150941B%40gmail.com.

Daniel Hepper

unread,
Dec 10, 2019, 6:54:58 AM12/10/19
to django...@googlegroups.com
It should be {% url 'polls:detail' question.id %}

I think you mixed up the steps "Removing hardcoded URLs in templates" and "Namespacing URL names"

Hope that helps,
Daniel

--

GGFU GAME

unread,
Dec 10, 2019, 7:21:08 AM12/10/19
to django...@googlegroups.com
Your code:
“<li><a href="{% url 'detail' question.id %}">{{ question.question_text }}</a></li>” 

Try addinga polls:detail like this:
“<li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>” 

--

Bruckner de Villiers

unread,
Dec 11, 2019, 2:35:10 AM12/11/19
to django...@googlegroups.com

Thank you Sencer.

 

 

Bruckner de Villiers

083 625 1086

 

Bruckner de Villiers

unread,
Dec 11, 2019, 2:35:28 AM12/11/19
to django...@googlegroups.com

Thank you Daniel.

 

Bruckner de Villiers

083 625 1086

 

From: <django...@googlegroups.com> on behalf of Daniel Hepper <daniel...@gmail.com>
Reply to: <django...@googlegroups.com>
Date: Tuesday, 10 December 2019 at 13:54
To: <django...@googlegroups.com>
Subject: Re: Removing Hardcoded urls in Templates

 

It should be {% url 'polls:detail' question.id %}

Reply all
Reply to author
Forward
0 new messages