TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:python:django

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:python:django [2024/08/29 07:56] – [Django Views] tungntdevelopment:python:django [2024/08/29 08:32] (current) – [Django Views] tungnt
Line 259: Line 259:
  
 <file python polls/views.py> <file python polls/views.py>
-from django.shortcuts import render+from django.shortcuts import get_object_or_404, render 
 +from django.template import loader
 from django.http import HttpResponse from django.http import HttpResponse
 +from django.http import Http404
 +
 +from .models import Question
  
 def index(request): def index(request):
-    return HttpResponse("HelloworldYou're at the polls index.")+    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)) 
 +    return render(request, "polls/index.html", context)
  
 def detail(request, question_id): def detail(request, question_id):
-    return HttpResponse("You're looking at question %s." % question_id)+    #try: 
 +    #    question = Question.objects.get(pk=question_id) 
 +    #except Question.DoesNotExist: 
 +    #    raise Http404("Question does not exist"
 +     
 +    question = get_object_or_404(Question, pk=question_id) 
 + 
 +    return render(request, "polls/detail.html", {"question": question})
  
  
Line 275: Line 292:
  
 def vote(request, question_id): def vote(request, question_id):
-    return HttpResponse("You're voting on question %s." % question_id)    +    return HttpResponse("You're voting on question %s." % question_id)     
 </file> </file>
  
Line 282: Line 299:
  
 from . import views from . import views
 +
 +app_name = "polls"
  
 urlpatterns = [ urlpatterns = [
Line 294: Line 313:
 ] ]
 </file>     </file>    
 +
 +<file html 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>-->
 +    <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
 +    {% endfor %}
 +</ul>
 +{% else %}
 +<p>No polls are available.</p>
 +{% endif %}
 +</file>
 +
 +<file html polls/templates/polls/detail.html>
 +<h1>{{ question.question_text }}</h1>
 +<ul>
 +    {% for choice in question.choice_set.all %}
 +    <li>{{ choice.choice_text }}</li>
 +    {% endfor %}
 +</ul>
 +</file>
 +
 +====== Django Form ======
 +
 +https://docs.djangoproject.com/en/5.1/intro/tutorial04/
 +
development/python/django.1724918203.txt.gz · Last modified: 2024/08/29 07:56 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki