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:51] – [Django Admin] tungntdevelopment:python:django [2024/08/29 08:32] (current) – [Django Views] tungnt
Line 140: Line 140:
 </code> </code>
  
-===== Django shell =====+====== Django Shell ======
  
 <code bash> <code bash>
Line 240: Line 240:
  
 ====== Django Admin ====== ====== Django Admin ======
 +
 +https://docs.djangoproject.com/en/5.1/intro/tutorial02/
  
 <code bash> <code bash>
Line 252: Line 254:
 </file> </file>
  
-====== Views ======+====== Django Views ======
  
 https://docs.djangoproject.com/en/5.1/intro/tutorial03/ https://docs.djangoproject.com/en/5.1/intro/tutorial03/
 +
 +<file python polls/views.py>
 +from django.shortcuts import get_object_or_404, render
 +from django.template import loader
 +from django.http import HttpResponse
 +from django.http import Http404
 +
 +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))
 +    return render(request, "polls/index.html", context)
 +
 +def detail(request, 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})
 +
 +
 +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)     
 +</file>
 +
 +<file python polls/urls.py>
 +from django.urls import path
 +
 +from . import views
 +
 +app_name = "polls"
 +
 +urlpatterns = [
 +    # ex: /polls/
 +    path("", views.index, name="index"),
 +    # ex: /polls/{id}/
 +    path("<int:question_id>/", views.detail, name="detail"),
 +    # ex: /polls/{id}/results/
 +    path("<int:question_id>/results/", views.results, name="results"),
 +    # ex: /polls/{id}/vote/
 +    path("<int:question_id>/vote/", views.vote, name="vote"),
 +]
 +</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.1724917885.txt.gz · Last modified: 2024/08/29 07:51 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki