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:33] – [Django shell] tungnt | development:python:django [2024/08/29 08:32] (current) – [Django Views] tungnt | ||
---|---|---|---|
Line 140: | Line 140: | ||
</ | </ | ||
- | ===== Django | + | ====== Django |
<code bash> | <code bash> | ||
Line 238: | Line 238: | ||
(1, {' | (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.1724916786.txt.gz · Last modified: 2024/08/29 07:33 by tungnt