|
| 1 | +Quick Walkthrough Tutorial |
| 2 | +-------------------------- |
| 3 | + |
| 4 | +This tutorial will take off where django official tutorial `Writing your first Django app, part 4 <django_tutorial_04_>`_ |
| 5 | +left off. The Question model has a datetime field. We are going to create a page to add new poll questions |
| 6 | +and a page to edit them. |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +.. tip:: We are going to use Bootstrap 4 here, if you are using Bootstrap 3 just replace the 4's with 3's in the |
| 11 | + codes and instructions below. |
| 12 | + |
| 13 | +Open up your console and install the following packages: |
| 14 | + |
| 15 | +:: |
| 16 | + |
| 17 | + pip install django-bootstrap4 |
| 18 | + pip install django-bootstrap-datepicker-plus |
| 19 | + |
| 20 | + |
| 21 | +Add these packages to the list of INSTALLED_APPS as you did here on `Tutorial 02 <django_tutorial_activating_model_>`_. |
| 22 | + |
| 23 | +.. code:: python |
| 24 | +
|
| 25 | + # FIle: mysite/settings.py |
| 26 | + INSTALLED_APPS = [ |
| 27 | + 'polls.apps.PollsConfig', |
| 28 | + 'django.contrib.admin', |
| 29 | + 'django.contrib.auth', |
| 30 | + 'django.contrib.contenttypes', |
| 31 | + 'django.contrib.sessions', |
| 32 | + 'django.contrib.messages', |
| 33 | + 'django.contrib.staticfiles', |
| 34 | + "bootstrap4", |
| 35 | + "bootstrap_datepicker_plus", |
| 36 | + ] |
| 37 | +
|
| 38 | +
|
| 39 | +CreateView for Question model |
| 40 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 41 | + |
| 42 | +Add a CreateView for Question model. The ``get_form`` method is used to specify widgets on the form fields. |
| 43 | + |
| 44 | +.. code:: python |
| 45 | +
|
| 46 | + # FIle: polls/views.py |
| 47 | + from django.http import HttpResponseRedirect |
| 48 | + from django.shortcuts import get_object_or_404, render |
| 49 | + from django.urls import reverse |
| 50 | + from django.views import generic |
| 51 | +
|
| 52 | + from bootstrap_datepicker_plus import DateTimePickerInput |
| 53 | +
|
| 54 | + from .models import Choice, Question |
| 55 | +
|
| 56 | +
|
| 57 | + class CreateView(generic.edit.CreateView): |
| 58 | + model = Question |
| 59 | + fields = ['question_text', 'pub_date'] |
| 60 | + def get_form(self): |
| 61 | + form = super().get_form() |
| 62 | + form.fields['pub_date'].widget = DateTimePickerInput() |
| 63 | + return form |
| 64 | + |
| 65 | + # Leave other classes unchanged |
| 66 | +
|
| 67 | +
|
| 68 | +Create a template named question_form.html in your app to render the form. If you use a different name you have to |
| 69 | +set template_name property of CreateView class in your views.py file above. |
| 70 | + |
| 71 | +.. code:: html |
| 72 | + |
| 73 | + <!-- File: polls/templates/polls/question_form.html --> |
| 74 | + {% load bootstrap4 %} |
| 75 | + {% bootstrap_css %} |
| 76 | + {% bootstrap_javascript jquery='full' %} |
| 77 | + {{ form.media }} |
| 78 | + |
| 79 | + <form method="post">{% csrf_token %} |
| 80 | + {% bootstrap_form form %} |
| 81 | + <input type="submit" value="Save"> |
| 82 | + </form> |
| 83 | + |
| 84 | +Add a ``get_absolute_url`` method to your Question model. |
| 85 | + |
| 86 | +.. code:: python |
| 87 | +
|
| 88 | + # FIle: polls/models.py |
| 89 | + import datetime |
| 90 | +
|
| 91 | + from django.db import models |
| 92 | + from django.urls import reverse |
| 93 | + from django.utils import timezone |
| 94 | +
|
| 95 | +
|
| 96 | + class Question(models.Model): |
| 97 | + question_text = models.CharField(max_length=200) |
| 98 | + pub_date = models.DateTimeField('date published') |
| 99 | +
|
| 100 | + def __str__(self): |
| 101 | + return self.question_text |
| 102 | +
|
| 103 | + def was_published_recently(self): |
| 104 | + return self.pub_date >= timezone.now() - datetime.timedelta(days=1) |
| 105 | + |
| 106 | + def get_absolute_url(self): |
| 107 | + return reverse('polls:detail', kwargs={'pk': self.pk}) |
| 108 | +
|
| 109 | +
|
| 110 | +Add an urlpattern for creating new poll question. |
| 111 | + |
| 112 | +.. code:: python |
| 113 | +
|
| 114 | + # FIle: polls/urls.py |
| 115 | + from django.urls import path |
| 116 | +
|
| 117 | + from . import views |
| 118 | +
|
| 119 | + app_name = 'polls' |
| 120 | + urlpatterns = [ |
| 121 | + path('', views.IndexView.as_view(), name='index'), |
| 122 | + path('create', views.CreateView.as_view(), name='create'), |
| 123 | + path('<int:pk>/', views.DetailView.as_view(), name='detail'), |
| 124 | + path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), |
| 125 | + path('<int:question_id>/vote/', views.vote, name='vote'), |
| 126 | + ] |
| 127 | +
|
| 128 | +
|
| 129 | +Now run the developement server and visit http://localhost:8000/polls/create, if everything works fine |
| 130 | +you can wrap up your template in proper HTML. |
| 131 | + |
| 132 | +.. code:: html |
| 133 | + |
| 134 | + <!DOCTYPE html> |
| 135 | + <html lang="en"> |
| 136 | + <head> |
| 137 | + <meta charset="UTF-8"> |
| 138 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 139 | + <meta http-equiv="X-UA-Compatible" content="ie=edge"> |
| 140 | + <title>Document</title> |
| 141 | + {% load bootstrap4 %} |
| 142 | + {% bootstrap_css %} |
| 143 | + {% bootstrap_javascript jquery='full' %} |
| 144 | + {{ form.media }} |
| 145 | + </head> |
| 146 | + <body> |
| 147 | + <div class="container"> |
| 148 | + <div class="col-md-3"> |
| 149 | + <form method="post">{% csrf_token %} |
| 150 | + {% bootstrap_form form %} |
| 151 | + {% buttons %} |
| 152 | + <button type="submit" class="btn btn-primary">Save</button> |
| 153 | + {% endbuttons %} |
| 154 | + </form> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </body> |
| 158 | + </html> |
| 159 | + |
| 160 | + |
| 161 | +UpdateView for Question model |
| 162 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 163 | + |
| 164 | +We can now add a page to update a poll question. First we add an UpdateView to our views. |
| 165 | + |
| 166 | +.. code:: python |
| 167 | +
|
| 168 | + # FIle: add these to polls/views.py |
| 169 | + class UpdateView(generic.edit.UpdateView): |
| 170 | + model = Question |
| 171 | + fields = ['question_text', 'pub_date'] |
| 172 | + def get_form(self): |
| 173 | + form = super().get_form() |
| 174 | + form.fields['pub_date'].widget = DateTimePickerInput() |
| 175 | + return form |
| 176 | +
|
| 177 | +Then add a urlpattern to access the question update page. |
| 178 | + |
| 179 | +.. code:: python |
| 180 | +
|
| 181 | + # FIle: polls/urls.py |
| 182 | + from django.urls import path |
| 183 | +
|
| 184 | + from . import views |
| 185 | +
|
| 186 | + app_name = 'polls' |
| 187 | + urlpatterns = [ |
| 188 | + path('', views.IndexView.as_view(), name='index'), |
| 189 | + path('create', views.CreateView.as_view(), name='create'), |
| 190 | + path('<int:pk>/', views.DetailView.as_view(), name='detail'), |
| 191 | + path('<int:pk>/update', views.UpdateView.as_view(), name='update'), |
| 192 | + path('<int:pk>/results/', views.ResultsView.as_view(), name='results'), |
| 193 | + path('<int:question_id>/vote/', views.vote, name='vote'), |
| 194 | + ] |
| 195 | +
|
| 196 | +That's it, run the developement server and visit http://localhost:8000/polls/1/update, if everything works fine |
| 197 | +you can checkout usage in custom form and model form in Usage page of the docs. |
| 198 | + |
| 199 | + |
| 200 | +.. _django_tutorial_04: https://docs.djangoproject.com/en/2.1/intro/tutorial04/ |
| 201 | +.. _django_tutorial_activating_model: https://docs.djangoproject.com/en/2.1/intro/tutorial02/#activating-models |
0 commit comments