Working through the Django tutorial and have run into a problem I cannot figure out. Help would be appreciated. What am I missing here? whenever is search the url "http://127.0.0.1:8000/polls/" the site refused to connect and in command prompt many e

50 views
Skip to first unread message

Avitab Ayan Sarmah

unread,
May 3, 2018, 3:37:47 PM5/3/18
to Django users
mysite/urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
path('', include('polls.urls')),
path('admin/', admin.site.urls),
]

polls/templates/polls/index.html:

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

polls/urls.py:

from django.urls import path

from . import views

urlpatterns = [
# ex: /polls/
path('', views.index, name='index'),
# ex: /polls/5/
path('<int:question_id>/', views.detail, name='detail'),
#ex: /polls/5/results/
path('<int:question_id>/results/', views.results, name='results'),
#ex: /polls/5/vote/
path('int:question_id>/vote/', views.vote, name='vote'),
]

polls/views.py:

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

from . models import Question

def index(request):
return HttpResponse("Hello, world.You're at the polls index.")
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
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've attached the command prompt below.Please check,your help would be very helpfull
Capture3.PNG

Fidel Leon

unread,
May 3, 2018, 3:50:48 PM5/3/18
to django...@googlegroups.com
El jue., 3 may. 2018 a las 17:38, Avitab Ayan Sarmah (<avita...@gmail.com>) escribió:

polls/views.py:

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

from . models import Question

def index(request):
return HttpResponse("Hello, world.You're at the polls index.")
latest_question_list = Question.objects.order_by('-pub_date')[:5]
output = ', '.join([q.question_text for q in latest_question_list])
return HttpResponse(output)
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))


Your function index(request) inside polls/views.py is badly written (you have three function returns). Seems you are following the tutorial but not removing the previous examples:

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)

--
Fidel Leon

Avitab Ayan Sarmah

unread,
May 3, 2018, 4:08:55 PM5/3/18
to django...@googlegroups.com
hello Fidel Leon can you please rewrite the whole views.py code so that i can understand what is the exact code is

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@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/CAHXg%3DN2SNuyAsAzk7mYfBB5rahtD3fRSa4vTbL92BQf5CHg-Nw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

Fidel Leon

unread,
May 3, 2018, 4:28:05 PM5/3/18
to django...@googlegroups.com
Sure:

from django.http import HttpResponse

from .models import Question


def index(request):
    latest_question_list = Question.objects.order_by('-pub_date')[:5]
    output = ', '.join([q.question_text for q in latest_question_list])
    return HttpResponse(output)
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)

As a matter of advice, please use a fixed-width font when pasting code, because Python is indented with spaces and using any other type of font makes reading your mail difficult :)

To unsubscribe from this group and all its topics, 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.

--
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.

For more options, visit https://groups.google.com/d/optout.


--

Avitab Ayan Sarmah

unread,
May 3, 2018, 4:36:57 PM5/3/18
to django...@googlegroups.com
thank you Fidel, and i will take care of it

To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

--
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+unsubscribe@googlegroups.com.


--

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Anthony Flury

unread,
May 3, 2018, 5:08:40 PM5/3/18
to django...@googlegroups.com
I think the root cause of the errors was due to an incorrect settings -
but Fidel is right that your views.py wasn't great either.

On 03/05/18 17:36, Avitab Ayan Sarmah wrote:
> thank you Fidel, and i will take care of it
>
> On Thu, May 3, 2018 at 9:57 PM, Fidel Leon <fi...@flm.cat
> <mailto:fi...@flm.cat>> wrote:
>
> Sure:
>
> from django.http import HttpResponse
>
> from .models import Question
>
>
> def index(request):
> latest_question_list = Question.objects.order_by('-pub_date')[:5]
>     output = ', '.join([q.question_text for q in
> latest_question_list])
>     return HttpResponse(output)
>
> 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)
>
> As a matter of advice, please use a fixed-width font when pasting
> code, because Python is indented with spaces and using any other
> type of font makes reading your mail difficult :)
>
> El jue., 3 may. 2018 a las 18:08, Avitab Ayan Sarmah
> (<avita...@gmail.com <mailto:avita...@gmail.com>>) escribió:
>
> hello Fidel Leon can you please rewrite the whole views.py
> code so that i can understand what is the exact code is
>
> On Thu, May 3, 2018 at 9:19 PM, Fidel Leon <fi...@flm.cat
> <mailto:fi...@flm.cat>> wrote:
>
>
>
> El jue., 3 may. 2018 a las 17:38, Avitab Ayan Sarmah
> (<avita...@gmail.com <mailto:avita...@gmail.com>>)
> escribió:
>
>
> *polls/views.py*:
> fi...@flm.cat <mailto:fi...@flm.cat>
>
> --
> You received this message because you are subscribed to a
> topic in the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe
> <https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe>.
> To unsubscribe from this group and all its topics, send an
> email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/CAHXg%3DN2SNuyAsAzk7mYfBB5rahtD3fRSa4vTbL92BQf5CHg-Nw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAEx5wm7VMMa3C2%3DyWA%3DQu%3DhihJiWujwvoA59Xx4ru59Zdc0UVg%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEx5wm7VMMa3C2%3DyWA%3DQu%3DhihJiWujwvoA59Xx4ru59Zdc0UVg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
>
> --
> Fidel Leon
> fi...@flm.cat <mailto:fi...@flm.cat>
> Phone: +34 622 26 44 92
> --
> You received this message because you are subscribed to a topic in
> the Google Groups "Django users" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe
> <https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe>.
> To unsubscribe from this group and all its topics, send an email
> to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> <https://groups.google.com/group/django-users>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAHXg%3DN3-8kKboqLD8cFRympuLvtTgj0uh6BxbfMr8FtfyNEojA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAHXg%3DN3-8kKboqLD8cFRympuLvtTgj0uh6BxbfMr8FtfyNEojA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
>
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/CAEx5wm6dhT3ZLPvKcFObV1Po26w82b3u%3DwFoj7GQm_iYUg2xzw%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAEx5wm6dhT3ZLPvKcFObV1Po26w82b3u%3DwFoj7GQm_iYUg2xzw%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Avitab Ayan Sarmah

unread,
May 3, 2018, 5:12:27 PM5/3/18
to django...@googlegroups.com
thank you Anthony, now i will try your code and i am sure it will run this time

            email to django-users+unsubscribe@googlegroups.com
            <mailto:django-users+unsubscrib...@googlegroups.com>.

            To post to this group, send email to
            django...@googlegroups.com
            <mailto:django-users@googlegroups.com>.



        --         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+unsubscribe@googlegroups.com
        <mailto:django-users+unsubscrib...@googlegroups.com>.

        To post to this group, send email to
        django...@googlegroups.com
        <mailto:django-users@googlegroups.com>.

        Visit this group at
        https://groups.google.com/group/django-users
        <https://groups.google.com/group/django-users>.
        To view this discussion on the web visit
        https://groups.google.com/d/msgid/django-users/CAEx5wm7VMMa3C2%3DyWA%3DQu%3DhihJiWujwvoA59Xx4ru59Zdc0UVg%40mail.gmail.com
        <https://groups.google.com/d/msgid/django-users/CAEx5wm7VMMa3C2%3DyWA%3DQu%3DhihJiWujwvoA59Xx4ru59Zdc0UVg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
        For more options, visit https://groups.google.com/d/optout
        <https://groups.google.com/d/optout>.



    --     Fidel Leon
    fi...@flm.cat <mailto:fi...@flm.cat>
    Phone: +34 622 26 44 92
    --     You received this message because you are subscribed to a topic in
    the Google Groups "Django users" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe
    <https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe>.

    To unsubscribe from this group and all its topics, send an email

    To post to this group, send email to django...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com <mailto:django-users+unsubscrib...@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com <mailto:django-users@googlegroups.com>.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.

James Farris

unread,
May 4, 2018, 1:11:42 AM5/4/18
to django...@googlegroups.com
In looks like there is a syntax error also in your polls/urls.py
Your missing the opening <
This:
path(‘int:question_id>/vote/', views.vote, name='vote'),

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

            email to django-users...@googlegroups.com
            <mailto:django-users...@googlegroups.com>.

            To post to this group, send email to
            django...@googlegroups.com
            <mailto:django...@googlegroups.com>.



        --         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
        <mailto:django-users...@googlegroups.com>.

        To post to this group, send email to
        django...@googlegroups.com
        <mailto:django...@googlegroups.com>.

        Visit this group at
        https://groups.google.com/group/django-users
        <https://groups.google.com/group/django-users>.
        To view this discussion on the web visit
        https://groups.google.com/d/msgid/django-users/CAEx5wm7VMMa3C2%3DyWA%3DQu%3DhihJiWujwvoA59Xx4ru59Zdc0UVg%40mail.gmail.com
        <https://groups.google.com/d/msgid/django-users/CAEx5wm7VMMa3C2%3DyWA%3DQu%3DhihJiWujwvoA59Xx4ru59Zdc0UVg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
        For more options, visit https://groups.google.com/d/optout
        <https://groups.google.com/d/optout>.



    --     Fidel Leon
    fi...@flm.cat <mailto:fi...@flm.cat>
    Phone: +34 622 26 44 92
    --     You received this message because you are subscribed to a topic in
    the Google Groups "Django users" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe
    <https://groups.google.com/d/topic/django-users/5hvN4Lfds-w/unsubscribe>.

    To unsubscribe from this group and all its topics, send an email

    To post to this group, send email to django...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com <mailto:django-users...@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com <mailto:django...@googlegroups.com>.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
--
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.

gibson...@u4sd.org

unread,
May 4, 2018, 3:25:20 PM5/4/18
to Django users
okay
Reply all
Reply to author
Forward
0 new messages