Tutorial writing views help

73 views
Skip to first unread message

Kareem Hart

unread,
Aug 11, 2017, 2:27:01 PM8/11/17
to Django users
I am currently up to the "writing views" part of the tutorial and just wrote the polls/index.html template. I updated the view in polls/view.py as such:

from django.http import HttpResponse
from django.template import loader

from .models import Question

def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    template = loader.get_template('polls/index.html')
    context = {
        'latest_question_list': latest_question_list,
    }
    return HttpResponse(template.render(context,request))

def detail(request,question_id):
    return HttpResponse("You're looking at question %s." % question_id)

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)




I ran  $python manage.py check and the system identified no issues.
When I open the page 127.0.0.1:8000/polls/ I  get a blank page instead of a bulleted list of the questions I created.

Also I get the following error when I load 127.0.0.1:8000/polls/.

Page not found (404)

Request Method:GET
Request URL:http://127.0.0.1:8000/

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

  1. ^polls/
  2. ^admin/

The empty path didn't match any of these.


mysite.urls


from django.conf.urls import url, include

from django.contrib import admin



urlpatterns = [

    url(r'^polls/', include('polls.urls')),

    url(r'^admin/', admin.site.urls),

]


Sol Chikuse

unread,
Aug 14, 2017, 3:52:06 AM8/14/17
to Django users
Hi Kareem,

Regarding the 404 page error, you do not have to put a dot (.) at the end of your URL. Django does not have such kind of a pattern match in your urls. 127.0.0.1:8000/polls/ is the correct URL. Having said that, how does your index template look like? It might be that you are getting a blank page because you are not declaring your variables correctly in the template itself. Meaning it might not be specifically a Django back-end error but a front-end html (template) error. Have a look at your template variables and tags and see if your data is correctly being presented to the template. You will need some code like:

{% if latest_question_list %}
{% for question in latest_question_list %}

 Have a look at the template documentation for details.

Kareem Hart

unread,
Aug 14, 2017, 2:45:05 PM8/14/17
to Django users
Here is my template.
 
index.html

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


Here is my command prompt log.

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

C:\Users\HP>cd mysite/

C:\Users\HP\mysite>python manage.py check
System check identified no issues (0 silenced).

C:\Users\HP\mysite>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
August 14, 2017 - 14:37:00
Django version 1.11.4, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[14/Aug/2017 14:37:06] "GET /polls/ HTTP/1.1" 200 25
Not Found: /favicon.ico
[14/Aug/2017 14:37:06] "GET /favicon.ico HTTP/1.1" 404 2069
Not Found: /
[14/Aug/2017 14:37:11] "GET / HTTP/1.1" 404 2018
Not Found: /
[14/Aug/2017 14:37:24] "GET / HTTP/1.1" 404 2018

Kareem Hart

unread,
Aug 14, 2017, 5:05:09 PM8/14/17
to Django users
I've found my mistake with the index.html file.

  {% for question in latest_question %}
        <li><a herf="/polls/{{question.id}}/">{{question.question_text}}</a></li> 

herf is suppose to be href.

But  http://127.0.0.1:8000/  still says page not found.

Matthew Pava

unread,
Aug 14, 2017, 5:28:07 PM8/14/17
to django...@googlegroups.com

And are you navigating to

http://127.0.0.1:8000/polls/

?  

 

I don’t think you should have anything at

http://127.0.0.1:8000/  

or so it seems.

--
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 post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5403110a-751c-4fae-84d5-0aa0906e14d4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Sol Chikuse

unread,
Aug 14, 2017, 5:33:41 PM8/14/17
to Django users
Hi Kareem,

In your template you have your code as: 

{% for question in latest_question %}

According to your context dictionary in your index view, the above template code should be:

{% for question in latest_question_list %}

And then you should go to the url 127.0.0.1:8000/polls/

Regards.
Reply all
Reply to author
Forward
0 new messages