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 08:02] – [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.template import loader
 from django.http import HttpResponse from django.http import HttpResponse
 +from django.http import Http404
  
 from .models import Question from .models import Question
Line 271: Line 272:
         "latest_question_list": latest_question_list,         "latest_question_list": latest_question_list,
     }     }
-    return HttpResponse(template.render(context, request))+    #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 290: Line 299:
  
 from . import views from . import views
 +
 +app_name = "polls"
  
 urlpatterns = [ urlpatterns = [
Line 307: Line 318:
 <ul> <ul>
     {% for question in latest_question_list %}     {% for question in latest_question_list %}
-    <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>+    <!--<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 %}     {% endfor %}
 </ul> </ul>
Line 314: Line 326:
 {% endif %} {% endif %}
 </file> </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.1724918577.txt.gz · Last modified: 2024/08/29 08:02 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki