user9890549
user9890549

Reputation:

I can't load polls/detail.html

hi guys I can't load polls/detail.html

I am currently studying Django tutorials part3

If I enter http: // localhost: 8000 / polls / index.html in the browser address bar, I get a page not found response

and The browser shows me the following text

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

polls/ [name='index']
polls/ <int:question_id>/ [name='detail']
polls/ <int:question_id>/results/ [name='results']
polls/ <int:question_id>/vote/ [name='vote']
admin/
The current path, polls/index.html, didn't match any of these

I think there's a problem with the route

this is my view.py code

from django.shortcuts import get_object_or_404, render

from .models import Question
#...
def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    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)

polls/tamplates/polls/detail.html

<h1>{{ question.question_text }}</h1>
<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }}</li>
{% endfor %}
</ul>

polls/templates/polls/index.html

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'detail' question.id %}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

Upvotes: 0

Views: 285

Answers (1)

Umair Mohammad
Umair Mohammad

Reputation: 4635

You can't access the index.html file directly by entering http://localhost:8000/polls/index.html because Django app doesn't work that way.

In Django we map routes, pattern of urls to corresponding view. When request comes to that url, the corresponding view is rendered.

These are the routes/urls defined by your router/urls mapper:

polls/ [name='index']
polls/ <int:question_id>/ [name='detail']
polls/ <int:question_id>/results/ [name='results']
polls/ <int:question_id>/vote/ [name='vote']
admin/

When request come to :

/polls/ : First pattern matched and it's corresponding view is returned which internally calls and render index.html

/polls/1/ : When you append question id, second pattern is matched and the detail view is called

/polls/1/results/ : When you append results also the result is rendered

and so on.

So you can't simply access index.html at http://localhost:8000/polls/index.html

It will only be served at the url pattern mapped to it. Which I think here is /polls/ whose namespace is index

Upvotes: 2

Related Questions