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:05] – [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 275: Line 276:
  
 def detail(request, question_id): def detail(request, question_id):
-    try: +    #try: 
-        question = Question.objects.get(pk=question_id) +    #    question = Question.objects.get(pk=question_id) 
-    except Question.DoesNotExist: +    #except Question.DoesNotExist: 
-        raise Http404("Question does not exist")+    #    raise Http404("Question does not exist") 
 +     
 +    question = get_object_or_404(Question, pk=question_id) 
     return render(request, "polls/detail.html", {"question": question})     return render(request, "polls/detail.html", {"question": question})
 +
  
 def results(request, question_id): def results(request, question_id):
Line 294: Line 299:
  
 from . import views from . import views
 +
 +app_name = "polls"
  
 urlpatterns = [ urlpatterns = [
Line 311: 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 320: Line 328:
  
 <file html polls/templates/polls/detail.html> <file html polls/templates/polls/detail.html>
-{{ question }}+<h1>{{ question.question_text }}</h1> 
 +<ul> 
 +    {% for choice in question.choice_set.all %} 
 +    <li>{{ choice.choice_text }}</li> 
 +    {% endfor %} 
 +</ul>
 </file> </file>
 +
 +====== Django Form ======
 +
 +https://docs.djangoproject.com/en/5.1/intro/tutorial04/
 +
development/python/django.1724918734.txt.gz · Last modified: 2024/08/29 08:05 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki