0% found this document useful (0 votes)
29 views4 pages

Django Part3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views4 pages

Django Part3

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Universidad Autónoma de Nuevo León

Facultad de Ciencias Físico Matemáticas


Ciencias Computacionales

Lenguajes Modernos de programación

JORGE ALBERTO ISLAS PINEDA

Daniel Alfredo Segura Palacios #2086108


Views.py

from django.shortcuts import render


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):


try:
question = Question.objects.get(pk=question_id)
except Question.DoesNotExist:
raise Http404("Question does not exist")
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)

detail.html

<h1>{{ question.question_text }}</h1>


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

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

Urls.py

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"),
]

You might also like