development:python:django
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:python:django [2024/08/29 07:25] – [Creating models] tungnt | development:python:django [2024/08/29 08:32] (current) – [Django Views] tungnt | ||
---|---|---|---|
Line 140: | Line 140: | ||
</ | </ | ||
- | ===== Django | + | ====== Django |
<code bash> | <code bash> | ||
Line 165: | Line 165: | ||
</ | </ | ||
+ | <file python polls/ | ||
+ | import datetime | ||
+ | from django.db import models | ||
+ | from django.utils import timezone | ||
+ | |||
+ | class Question(models.Model): | ||
+ | question_text = models.CharField(max_length=200) | ||
+ | pub_date = models.DateTimeField(" | ||
+ | |||
+ | def __str__(self): | ||
+ | return self.question_text | ||
+ | |||
+ | def was_published_recently(self): | ||
+ | return self.pub_date >= timezone.now() - datetime.timedelta(days=1) | ||
+ | |||
+ | class Choice(models.Model): | ||
+ | question = models.ForeignKey(Question, | ||
+ | choice_text = models.CharField(max_length=200) | ||
+ | votes = models.IntegerField(default=0) | ||
+ | |||
+ | def __str__(self): | ||
+ | return self.choice_text | ||
+ | </ | ||
+ | |||
+ | <code bash> | ||
+ | (django5.1) tungnt@MacBook-Pro-cua-Nguyen-2 django_blue % python manage.py shell | ||
+ | Python 3.11.6 (main, Oct 2 2023, 13:45:54) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin | ||
+ | Type " | ||
+ | (InteractiveConsole) | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | Traceback (most recent call last): | ||
+ | ... | ||
+ | raise self.model.DoesNotExist( | ||
+ | polls.models.Question.DoesNotExist: | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | True | ||
+ | >>> | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | <Choice: Not much> | ||
+ | >>> | ||
+ | <Choice: The sky> | ||
+ | >>> | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | 3 | ||
+ | >>> | ||
+ | < | ||
+ | >>> | ||
+ | >>> | ||
+ | (1, {' | ||
+ | </ | ||
+ | |||
+ | ====== Django Admin ====== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | <code bash> | ||
+ | % python manage.py createsuperuser | ||
+ | </ | ||
+ | |||
+ | <file python polls/ | ||
+ | from django.contrib import admin | ||
+ | from .models import Question | ||
+ | |||
+ | admin.site.register(Question) | ||
+ | </ | ||
+ | |||
+ | ====== Django Views ====== | ||
+ | |||
+ | https:// | ||
+ | |||
+ | <file python polls/ | ||
+ | from django.shortcuts import get_object_or_404, | ||
+ | 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(" | ||
+ | template = loader.get_template(" | ||
+ | context = { | ||
+ | " | ||
+ | } | ||
+ | #return HttpResponse(template.render(context, | ||
+ | return render(request, | ||
+ | |||
+ | def detail(request, | ||
+ | #try: | ||
+ | # question = Question.objects.get(pk=question_id) | ||
+ | #except Question.DoesNotExist: | ||
+ | # raise Http404(" | ||
+ | | ||
+ | question = get_object_or_404(Question, | ||
+ | |||
+ | return render(request, | ||
+ | |||
+ | |||
+ | def results(request, | ||
+ | response = " | ||
+ | return HttpResponse(response % question_id) | ||
+ | |||
+ | |||
+ | def vote(request, | ||
+ | return HttpResponse(" | ||
+ | </ | ||
+ | |||
+ | <file python polls/ | ||
+ | from django.urls import path | ||
+ | |||
+ | from . import views | ||
+ | |||
+ | app_name = " | ||
+ | |||
+ | urlpatterns = [ | ||
+ | # ex: /polls/ | ||
+ | path("", | ||
+ | # ex: / | ||
+ | path("< | ||
+ | # ex: / | ||
+ | path("< | ||
+ | # ex: / | ||
+ | path("< | ||
+ | ] | ||
+ | </ | ||
+ | |||
+ | <file html polls/ | ||
+ | {% if latest_question_list %} | ||
+ | <ul> | ||
+ | {% for question in latest_question_list %} | ||
+ | < | ||
+ | < | ||
+ | {% endfor %} | ||
+ | </ul> | ||
+ | {% else %} | ||
+ | <p>No polls are available.</ | ||
+ | {% endif %} | ||
+ | </ | ||
+ | |||
+ | <file html polls/ | ||
+ | < | ||
+ | <ul> | ||
+ | {% for choice in question.choice_set.all %} | ||
+ | < | ||
+ | {% endfor %} | ||
+ | </ul> | ||
+ | </ | ||
+ | |||
+ | ====== Django Form ====== | ||
+ | |||
+ | https:// | ||
development/python/django.1724916320.txt.gz · Last modified: 2024/08/29 07:25 by tungnt