| 26 | | A quick rundown: |
|---|
| 27 | | |
|---|
| 28 | | * The above template displays a radio button for each poll choice. The |
|---|
| 29 | | ``value`` of each radio button is the associated poll choice's ID. The |
|---|
| 30 | | ``name`` of each radio button is ``"choice"``. That means, when somebody |
|---|
| 31 | | selects one of the radio buttons and submits the form, it'll send the |
|---|
| 32 | | POST data ``choice=3``. This is HTML Forms 101. |
|---|
| 33 | | |
|---|
| 34 | | * We set the form's ``action`` to ``/polls/{{ poll.id }}/vote/``, and we |
|---|
| 35 | | set ``method="post"``. Using ``method="post"`` (as opposed to |
|---|
| 36 | | ``method="get"``) is very important, because the act of submitting this |
|---|
| 37 | | form will alter data server-side. Whenever you create a form that alters |
|---|
| 38 | | data server-side, use ``method="post"``. This tip isn't specific to |
|---|
| 39 | | Django; it's just good Web development practice. |
|---|
| 40 | | |
|---|
| 41 | | Now, let's create a Django view that handles the submitted data and does |
|---|
| 42 | | something with it. Remember, in `Tutorial 3`_, we created a URLconf for the |
|---|
| 43 | | polls application that includes this line:: |
|---|
| | 25 | Szybkie wyjaśnienie: |
|---|
| | 26 | |
|---|
| | 27 | * Powyższy szablon wyświetla przycisk typu ``"radio button"`` dla każdego wyboru ankiety. Pole ``value`` dla każdego radio button'a jest skojarzone z ID obiektu ``choice``. Atrybut ``name`` każdego radio button'a jest ``"choice"``. To oznacza, że jak ktoś wybiera jeden z radio button'ów i submit'uje formularz form, to wysłane zostaną dane za pomocą POST'a ``choice=3``. |
|---|
| | 28 | |
|---|
| | 29 | * Ustawiliśmy atrybut formularza ``action`` na ``/polls/{{ poll.id }}/vote/``, oraz ``method="post"``. Używanie ``method="post"`` (Przeciwne do ``method="get"``) jest bardzo ważne, ponieważ submit'owanie formularza zmieni dane po stronie serwera. Jeżeli kiedykolwiek tworzysz formularz który zmienia dane po stronie serwera, używaj ``method="post"``. Ta wskazówka nie ma nic wspólnego z Django, jest to dobra praktyka tworzenia aplikacji Web'owych. |
|---|
| | 30 | |
|---|
| | 31 | * ``forloop.counter`` oznacza ile razy tag ``for`` wykonał kod wewnątrz pętli. Więcej informacji znajdziesz w `dokumentacji dla tag'a "for"`_. |
|---|
| | 32 | |
|---|
| | 33 | .. _dokumentacji dla tag'a "for": http://www.djangoproject.com/documentation/templates/#for |
|---|
| | 34 | |
|---|
| | 35 | Teraz stwórzmy widok Django który obsługuje submit'owane dane oraz coś z nimi robi. |
|---|
| | 36 | Pamiętaj, że w `tutorial'u nr 3`, stworzyliśmy URLconf dla aplikacji pools, która zawiera linie:: |
|---|
| 72 | | This code includes a few things we haven't covered yet in this tutorial: |
|---|
| 73 | | |
|---|
| 74 | | * ``request.POST`` is a dictionary-like object that lets you access |
|---|
| 75 | | submitted data by key name. In this case, ``request.POST['choice']`` |
|---|
| 76 | | returns the ID of the selected choice, as a string. ``request.POST`` |
|---|
| 77 | | values are always strings. |
|---|
| 78 | | |
|---|
| 79 | | Note that Django also provides ``request.GET`` for accessing GET data |
|---|
| 80 | | in the same way -- but we're explicitly using ``request.POST`` in our |
|---|
| 81 | | code, to ensure that data is only altered via a POST call. |
|---|
| 82 | | |
|---|
| 83 | | * ``request.POST['choice']`` will raise ``KeyError`` if ``choice`` wasn't |
|---|
| 84 | | provided in POST data. The above code checks for ``KeyError`` and |
|---|
| 85 | | redisplays the poll form with an error message if ``choice`` isn't given. |
|---|
| 86 | | |
|---|
| 87 | | * After incrementing the choice count, the code returns an |
|---|
| 88 | | ``HttpResponseRedirect`` rather than a normal ``HttpResponse``. |
|---|
| 89 | | ``HttpResponseRedirect`` takes a single argument: the URL to which the |
|---|
| 90 | | user will be redirected (see the following point for how we construct |
|---|
| 91 | | the URL in this case). |
|---|
| 92 | | |
|---|
| 93 | | As the Python comment above points out, you should always return an |
|---|
| 94 | | ``HttpResponseRedirect`` after successfully dealing with POST data. This |
|---|
| 95 | | tip isn't specific to Django; it's just good Web development practice. |
|---|
| 96 | | |
|---|
| 97 | | * We are using the ``reverse()`` function in the ``HttpResponseRedirect`` |
|---|
| 98 | | constructor in this example. This function helps avoid having to |
|---|
| 99 | | hardcode a URL in the view function. It is given the name of the view |
|---|
| 100 | | that we want to pass control to and the variable portion of the URL |
|---|
| 101 | | pattern that points to that view. In this case, using the URLConf we set |
|---|
| 102 | | up in Tutorial 3, this ``reverse()`` call will return a string like :: |
|---|
| | 65 | Ten kod zawiera kilka rzeczy o których jeszcze nie mowiliśmy w tym tutorial'u: |
|---|
| | 66 | |
|---|
| | 67 | * ``request.POST`` jest obiektem ala słownik, który pozwala na dostęp do submitowanych danych po nazwie klucza. |
|---|
| | 68 | W tym przypadku, ``request.POST['choice']`` |
|---|
| | 69 | zwraca ID wybranej opcji ankiety, jako string. Wartości z ``request.POST`` |
|---|
| | 70 | są zawsze string'ami. |
|---|
| | 71 | |
|---|
| | 72 | Django także dostarcza ``request.GET`` do pobierania danych z GET'a |
|---|
| | 73 | w ten sam sposób -- jednak używamy ``request.POST`` w naszym kodzie, |
|---|
| | 74 | aby upewnić się, że dane są tylko zmienione przez wywołanie POST. |
|---|
| | 75 | |
|---|
| | 76 | * ``request.POST['choice']`` rzuci wyjątek ``KeyError`` jeżeli ``choice`` |
|---|
| | 77 | nie byl dostarczony w danych z POST'a. |
|---|
| | 78 | Powyższy kod oczekuje wyjątku ``KeyError`` oraz wyświetla ponownie |
|---|
| | 79 | formularz ankiety z informacją o błędzie jeżeli ``choice`` nie był podany. |
|---|
| | 80 | |
|---|
| | 81 | * Po zwiększeniu liczny głosów(``selected_choice.votes += 1``), kod zwraca ``HttpResponseRedirect`` |
|---|
| | 82 | zamiast zwykłego ``HttpResponse``. |
|---|
| | 83 | ``HttpResponseRedirect`` przyjmuje pojedynczy argument: URL do którego |
|---|
| | 84 | użytkownik zostanie przekierowany(zobacz kolejny punkt jak tworzymy URL w tym przypadku). |
|---|
| | 85 | |
|---|
| | 86 | Zgodnie z komentarzem w kodzie, powinieneś zawsze zwracać ``HttpResponseRedirect`` |
|---|
| | 87 | po udanym obsłużeniu danych z POST'a. To nie jest specyficzne dla Django; |
|---|
| | 88 | to po prostu dobra praktyka przy tworzeniu aplikacji web'owych. |
|---|
| | 89 | |
|---|
| | 90 | * Używamy funkcji ``reverse()`` w przekierowaniu ``HttpResponseRedirect``. |
|---|
| | 91 | Ta funkcja pomaga uniknąć sztywnego wpisywania URL'a w funkcji widoku. |
|---|
| | 92 | Jako parametr przekazujemy nazwę widoku, który obsłuży nam zapytanie |
|---|
| | 93 | oraz zmienne które są użyte w tym URL'u, które wskazują na ten widok. |
|---|
| | 94 | W tym przypadku, używając URLConf który ustawiliśmy w tutorial'u 3, |
|---|
| | 95 | wywołanie ``reverse()`` zwróci string podobny do tego:: |
|---|
| 106 | | ... where the ``3`` is the value of ``p.id``. This redirected URL will |
|---|
| 107 | | then call the ``'results'`` view to display the final page. Note that |
|---|
| 108 | | you need to use the full name of the view here (including the prefix). |
|---|
| 109 | | |
|---|
| 110 | | For more information about ``reverse()``, see the `URL dispatcher`_ |
|---|
| 111 | | documentation. |
|---|
| 112 | | |
|---|
| 113 | | As mentioned in Tutorial 3, ``request`` is a ``HTTPRequest`` object. For more |
|---|
| 114 | | on ``HTTPRequest`` objects, see the `request and response documentation`_. |
|---|
| 115 | | |
|---|
| 116 | | After somebody votes in a poll, the ``vote()`` view redirects to the results |
|---|
| 117 | | page for the poll. Let's write that view:: |
|---|
| | 99 | ... gdzie ``3`` jest wartością ``p.id``. Ten przekierowany URL wtedy |
|---|
| | 100 | wywoła widok ``'results'``, który wyświetli końcową stronę. Zwróć uwagę, |
|---|
| | 101 | że musisz tutaj użyć pełnej nazwy widoku(łącznie z prefiksem). |
|---|
| | 102 | |
|---|
| | 103 | Więcej informacji o ``reverse()``, możesz znaleźć w `dokumentacji URL dispatcher'a`_. |
|---|
| | 104 | |
|---|
| | 105 | Jak wspomniano w tutorial'u 3, ``request`` jest obiektem ``HTTPRequest``. Więcej |
|---|
| | 106 | o obiektach ``HTTPRequest``, zobacz w `dokumentacji request and response`_. |
|---|
| | 107 | |
|---|
| | 108 | Po tym jak ktoś zagłosuje w ankiecie, widok ``vote()`` przekierowuje do strony z wynikami ankiety. |
|---|
| | 109 | Napiszmy widok:: |
|---|
| 136 | | Now, go to ``/polls/1/`` in your browser and vote in the poll. You should see a |
|---|
| 137 | | results page that gets updated each time you vote. If you submit the form |
|---|
| 138 | | without having chosen a choice, you should see the error message. |
|---|
| 139 | | |
|---|
| 140 | | .. _request and response documentation: ../request_response/ |
|---|
| 141 | | .. _URL dispatcher: ../url_dispatch#reverse |
|---|
| 142 | | |
|---|
| 143 | | Use generic views: Less code is better |
|---|
| 144 | | ====================================== |
|---|
| 145 | | |
|---|
| 146 | | The ``detail()`` (from `Tutorial 3`_) and ``results()`` views are stupidly |
|---|
| 147 | | simple -- and, as mentioned above, redundant. The ``index()`` view (also from |
|---|
| 148 | | Tutorial 3), which displays a list of polls, is similar. |
|---|
| 149 | | |
|---|
| 150 | | These views represent a common case of basic Web development: getting data from |
|---|
| 151 | | the database according to a parameter passed in the URL, loading a template and |
|---|
| 152 | | returning the rendered template. Because this is so common, Django provides a |
|---|
| 153 | | shortcut, called the "generic views" system. |
|---|
| 154 | | |
|---|
| 155 | | Generic views abstract common patterns to the point where you don't even need |
|---|
| 156 | | to write Python code to write an app. |
|---|
| 157 | | |
|---|
| 158 | | Let's convert our poll app to use the generic views system, so we can delete a |
|---|
| 159 | | bunch of our own code. We'll just have to take a few steps to make the |
|---|
| 160 | | conversion. |
|---|
| 161 | | |
|---|
| 162 | | .. admonition:: Why the code-shuffle? |
|---|
| 163 | | |
|---|
| 164 | | Generally, when writing a Django app, you'll evaluate whether generic views |
|---|
| 165 | | are a good fit for your problem, and you'll use them from the beginning, |
|---|
| 166 | | rather than refactoring your code halfway through. But this tutorial |
|---|
| 167 | | intentionally has focused on writing the views "the hard way" until now, to |
|---|
| 168 | | focus on core concepts. |
|---|
| 169 | | |
|---|
| 170 | | You should know basic math before you start using a calculator. |
|---|
| 171 | | |
|---|
| 172 | | First, open the polls/urls.py URLconf. It looks like this, according to the |
|---|
| 173 | | tutorial so far:: |
|---|
| | 128 | Teraz otwórz ``/polls/1/`` w Twojej przeglądarce i zagłosój w ankiecie. Powinieneś zobaczyć stronę z wynikami, |
|---|
| | 129 | która jest uaktualniona za każdym razem jak głosujesz. Jeżeli submit'ujesz formularz bez wyboru na co głosujesz, |
|---|
| | 130 | powinieneś zobaczyć informacje o błędzie. |
|---|
| | 131 | |
|---|
| | 132 | .. _dokumentacji request and response: http://www.djangoproject.com/documentation/request_response/ |
|---|
| | 133 | .. _dokumentacji URL dispatcher'a: http://www.djangoproject.com/documentation/url_dispatch#reverse |
|---|
| | 134 | |
|---|
| | 135 | Użyj widoków generycznych: Im mniej kodu tym lepiej |
|---|
| | 136 | =================================================== |
|---|
| | 137 | |
|---|
| | 138 | Widoki ``detail()`` (z `tutorial'a nr 3`_) oraz ``results()`` są po prostu głupio |
|---|
| | 139 | proste -- i jak wspomniano wczesniej, nadmiarowe. Widok ``index()`` (także z |
|---|
| | 140 | tutorial'a nr 3), który wyświetla listę ankiet, jest podobny. |
|---|
| | 141 | |
|---|
| | 142 | Te widoki wykonują często powtarzane czynności w aplikacji www: pobranie danych |
|---|
| | 143 | z bazy danych zgodnie z parametrem przekazanym w URL'u, załadowaniem szablonu |
|---|
| | 144 | oraz zwróceniem przetworzonego szablonu. Ponieważ jest to tak często wykonywane, |
|---|
| | 145 | Django dostarcza skrót, zwany systemem "widoków generycznych". |
|---|
| | 146 | |
|---|
| | 147 | Widoki generyczne są abstrakcją do punktu w którym nie musisz nawet pisać |
|---|
| | 148 | kodu w Pythonie aby napisać aplikacje. |
|---|
| | 149 | |
|---|
| | 150 | Przeróbmy teraz naszą aplikacje ankiet aby używała systemu widoków generycznych, |
|---|
| | 151 | możemy więc skasować trochę kodu który napisaliśmy wcześniej. Musimy tylko wykonać |
|---|
| | 152 | kilka kroków aby dokonać tej konwersji. |
|---|
| | 153 | |
|---|
| | 154 | .. admonition:: Dlaczego zmieniamy w ten sposób kod ? |
|---|
| | 155 | |
|---|
| | 156 | Generalnie, gdy piszesz aplikacje w Django, musisz ocenić czy widoki generyczne |
|---|
| | 157 | są dobrym rozwiązaniem Twojego problemu, jeżeli tak, to będziesz ich używać od |
|---|
| | 158 | samego początku, niż zmieniał kod na końcu pisania aplikacji. Jednak w tym tutorial'u |
|---|
| | 159 | celowo skupiamy sie na pisaniu widoków od podstaw, aby pokazać podstawową koncepcje. |
|---|
| | 160 | |
|---|
| | 161 | Powinieneś znać podstawy matematyki, aby móc korzystać z kalkulatora. |
|---|
| | 162 | |
|---|
| | 163 | Najpierw, otwórz plik ustawień URL'i polls/urls.py. Wygląda mniej więcej tak, zgodnie z tym co napisaliśmy do tej pory:: |
|---|
| 200 | | We're using two generic views here: ``object_list`` and ``object_detail``. |
|---|
| 201 | | Respectively, those two views abstract the concepts of "display a list of |
|---|
| 202 | | objects" and "display a detail page for a particular type of object." |
|---|
| 203 | | |
|---|
| 204 | | * Each generic view needs to know what data it will be acting upon. This |
|---|
| 205 | | data is provided in a dictionary. The ``queryset`` key in this dictionary |
|---|
| 206 | | points to the list of objects to be manipulated by the generic view. |
|---|
| 207 | | |
|---|
| 208 | | * The ``object_detail`` generic view expects the ID value captured |
|---|
| 209 | | from the URL to be called ``"object_id"``, so we've changed ``poll_id`` to |
|---|
| 210 | | ``object_id`` for the generic views. |
|---|
| 211 | | |
|---|
| 212 | | * We've added a name, ``poll_results``, to the results view so that we |
|---|
| 213 | | have a way to refer to its URL later on (see the documentation about |
|---|
| 214 | | `naming URL patterns`_ for information). We're also using the `url()`_ |
|---|
| 215 | | function from ``django.conf.urls.defaults`` here. It's a good habit to |
|---|
| 216 | | use ``url()`` when you are providing a pattern name like this. |
|---|
| 217 | | |
|---|
| 218 | | .. _naming URL patterns: ../url_dispatch/#naming-url-patterns |
|---|
| | 190 | |
|---|
| | 191 | Używamy tutaj dwóch widoków generycznych: ``object_list`` oraz ``object_detail``. Te dwa widoki sa abstrakcja koncepcji "wyswietl liste obiektów" oraz "wyświetl stronę ze szczególami dla konkretnego typu obiektu". |
|---|
| | 192 | |
|---|
| | 193 | * Każdy widok generyczny musi wiedzieć na jakich danych będzie działać. Te dane podaje się w słowniku. Klucz ``queryset`` w tym słowniku wskazuje na listę obiektów, które będa manipulowane przez generyczny widok. |
|---|
| | 194 | |
|---|
| | 195 | * Widok generyczny ``object_detail`` oczekuje wartości ID przekazanej z URL'a, która nazywa się ``"object_id"``, wiec zmieniliśmy ``poll_id`` na ``object_id`` dla widoków generycznych. |
|---|
| | 196 | |
|---|
| | 197 | * Dodaliśmy nazwę ``poll_results``, do widoku wyników, aby mieć możliwość odwołania się do jego URL'a w późniejszym czasie (zobacz dokumentacje na temat `konfigurowania wzorców URL'li`_ aby uzyskać więcej informacji). |
|---|
| | 198 | |
|---|
| | 199 | Używamy tutaj także funkcji `url()`_ z pakietu ``django.conf.urls.defaults``. Jest to dobry zwyczaj |
|---|
| | 200 | aby używać ``url()`` kiedy podaje wzorzec taki jak tutaj. |
|---|
| | 201 | |
|---|
| | 202 | .. _konfigurowania wzorców URL'li: ../url_dispatch/#naming-url-patterns |
|---|
| 221 | | By default, the ``object_detail`` generic view uses a template called |
|---|
| 222 | | ``<app name>/<model name>_detail.html``. In our case, it'll use the template |
|---|
| 223 | | ``"polls/poll_detail.html"``. Thus, rename your ``polls/detail.html`` template to |
|---|
| 224 | | ``polls/poll_detail.html``, and change the ``render_to_response()`` line in |
|---|
| 225 | | ``vote()``. |
|---|
| 226 | | |
|---|
| 227 | | Similarly, the ``object_list`` generic view uses a template called |
|---|
| 228 | | ``<app name>/<model name>_list.html``. Thus, rename ``polls/index.html`` to |
|---|
| | 205 | Domyśnie, widok generyczny ``object_detail`` używa szablonu nazywającego się |
|---|
| | 206 | ``<nazwa aplikacji>/<nazwa modelu>_detail.html``. W naszym przypadku, używa szablonu |
|---|
| | 207 | ``"polls/poll_detail.html"``. Zmień nazwę Twojego szablonu, ``polls/detail.html`` na |
|---|
| | 208 | ``polls/poll_detail.html``, oraz zmień linie ``render_to_response()`` w ``vote()``. |
|---|
| | 209 | |
|---|
| | 210 | Podobnie, widok generyczny ``object_list`` używa szablonu nazwywającego się |
|---|
| | 211 | ``<nazwa aplikacji>/<nazwa modelu>_list.html``. Także, zmień ``polls/index.html`` na |
|---|
| 231 | | Because we have more than one entry in the URLconf that uses ``object_detail`` |
|---|
| 232 | | for the polls app, we manually specify a template name for the results view: |
|---|
| 233 | | ``template_name='polls/results.html'``. Otherwise, both views would use the same |
|---|
| 234 | | template. Note that we use ``dict()`` to return an altered dictionary in place. |
|---|
| 235 | | |
|---|
| 236 | | .. note:: ``all()`` is lazy |
|---|
| 237 | | |
|---|
| 238 | | It might look a little frightening to see ``Poll.objects.all()`` being used |
|---|
| 239 | | in a detail view which only needs one ``Poll`` object, but don't worry; |
|---|
| 240 | | ``Poll.objects.all()`` is actually a special object called a ``QuerySet``, |
|---|
| 241 | | which is "lazy" and doesn't hit your database until it absolutely has to. By |
|---|
| 242 | | the time the database query happens, the ``object_detail`` generic view will |
|---|
| 243 | | have narrowed its scope down to a single object, so the eventual query will |
|---|
| 244 | | only select one row from the database. |
|---|
| 245 | | |
|---|
| 246 | | If you'd like to know more about how that works, The Django database API |
|---|
| 247 | | documentation `explains the lazy nature of QuerySet objects`_. |
|---|
| 248 | | |
|---|
| 249 | | .. _explains the lazy nature of QuerySet objects: ../db-api/#querysets-are-lazy |
|---|
| 250 | | |
|---|
| 251 | | In previous parts of the tutorial, the templates have been provided with a context |
|---|
| 252 | | that contains the ``poll`` and ``latest_poll_list`` context variables. However, |
|---|
| 253 | | the generic views provide the variables ``object`` and ``object_list`` as context. |
|---|
| 254 | | Therefore, you need to change your templates to match the new context variables. |
|---|
| 255 | | Go through your templates, and modify any reference to ``latest_poll_list`` to |
|---|
| 256 | | ``object_list``, and change any reference to ``poll`` to ``object``. |
|---|
| 257 | | |
|---|
| 258 | | You can now delete the ``index()``, ``detail()`` and ``results()`` views |
|---|
| 259 | | from ``polls/views.py``. We don't need them anymore -- they have been replaced |
|---|
| 260 | | by generic views. |
|---|
| 261 | | |
|---|
| 262 | | The ``vote()`` view is still required. However, it must be modified to match |
|---|
| 263 | | the new templates and context variables. Change the template call from |
|---|
| 264 | | ``polls/detail.html`` to ``polls/poll_detail.html``, and pass ``object`` in the |
|---|
| 265 | | context instead of ``poll``. |
|---|
| 266 | | |
|---|
| 267 | | The last thing to do is fix the URL handling to account for the use of generic |
|---|
| 268 | | views. In the vote view above, we used the ``reverse()`` function to avoid |
|---|
| 269 | | hard-coding our URLs. Now that we've switched to a generic view, we'll need to |
|---|
| 270 | | change the ``reverse()`` call to point back to our new generic view. We can't |
|---|
| 271 | | simply use the view function anymore -- generic views can be (and are) used |
|---|
| 272 | | multiple times -- but we can use the name we've given:: |
|---|
| 273 | | |
|---|
| | 214 | Ponieważ mamy więcej niż jeden wpis w URLconf który używa ``object_detail`` |
|---|
| | 215 | dla aplikacji polls, określamu recznie nazwę szablonu dla widoku wyników: |
|---|
| | 216 | ``template_name='polls/results.html'``. W przeciwnym razie obydwa widoki używały by takiego samego szablonu. |
|---|
| | 217 | Zauwarz że używamy ``dict()`` aby zwrócić zmieniony słownik w tym miejscu. |
|---|
| | 218 | |
|---|
| | 219 | .. note:: ``all()`` jest leniwe |
|---|
| | 220 | |
|---|
| | 221 | To może wyglądać troche przerażające, wywołanie ``Poll.objects.all()`` |
|---|
| | 222 | w widoku szczegółu, który tylko potrzebuje jednego obiektu ``Poll``, ale nie martw się; |
|---|
| | 223 | ``Poll.objects.all()`` jest właściwie specjalnym obiektem nazwywanym ``QuerySet``, |
|---|
| | 224 | który jest "leniwy" i nie wyciąga nic z bazy danych dopóki nie musi. |
|---|
| | 225 | W czasie gdy wystąpi zapytanie do bazy, widok generyczny ``object_detail`` będzie miał |
|---|
| | 226 | zmniejszony swój zasięg do pojedyńczego obiektu, więc ewentualne zapytanie |
|---|
| | 227 | wybierze tylko jeden wiersz z bazy danych. |
|---|
| | 228 | |
|---|
| | 229 | Jezeli chciałbys sie dowiedzieć jak to działa, dokumentacja Django na tamat API |
|---|
| | 230 | bazy danych `wyjaśnia leniwą natrurę obiektów QuerySet`_. |
|---|
| | 231 | |
|---|
| | 232 | .. _wyjaśnia leniwą natrurę obiektów QuerySet: http://www.djangoproject.com/documentation/db-api/#querysets-are-lazy |
|---|
| | 233 | |
|---|
| | 234 | W porzednich częściach tego tutoriala, szablony miały podany kontkst który zawierał |
|---|
| | 235 | zmienne ``poll`` oraz ``latest_poll_list``. Jednak, widoki generyczne dostarczają |
|---|
| | 236 | zmienne ``object`` oraz ``object_list`` jako kontekst. |
|---|
| | 237 | Dlatego musisz zmienić Twoje szablony aby zawierały nowe zmienne kontekstu. |
|---|
| | 238 | Przejrzyj Twoje szablony, i zmodyfikuj każde odnieśienie do ``latest_poll_list`` na |
|---|
| | 239 | ``object_list``, i zmien każde wystąpienie ``poll`` na ``object``. |
|---|
| | 240 | |
|---|
| | 241 | Możesz teraz usunąc widoki ``index()``, ``detail()`` oraz ``results()`` z ``polls/views.py``. |
|---|
| | 242 | Już ich nie potrzebujemy -- zostały zastapione przez widoki generyczne. |
|---|
| | 243 | |
|---|
| | 244 | Widok ``vote()`` jest jednak ciągle potrzebny. Musi być jednakże zmodyfikowany, |
|---|
| | 245 | aby odpowiadał nowemu kontekstowi zmiennych. W wywołaniu ``render_to_response()``, zmień nazwę zmiennj |
|---|
| | 246 | ``poll`` na ``object``. |
|---|
| | 247 | |
|---|
| | 248 | Ostania rzecz do zrobienia to naprawa obsługi URL'i z uwzględnieniem użycia widoków generycznych. |
|---|
| | 249 | W widoku vote powyrzej, użylismy funkcji ``reverse()``, aby uniknąć sztywnego wpisywania URL'i w widokach. |
|---|
| | 250 | Teraz przełączyliśmy się na widok generyczny, musimy zmienić wywołanie ``reverse()`` aby wskazywało spowrotem do naszego widoku generycznego. Nie możemy już poprostu użyć funkcji widoku -- widoki genryczne mogą (i są) używane wielokrotnie -- |
|---|
| | 251 | ale możemy użyć nazwy której użyliśmy:: |
|---|
| | 252 | |
|---|
| 276 | | Run the server, and use your new polling app based on generic views. |
|---|
| 277 | | |
|---|
| 278 | | For full details on generic views, see the `generic views documentation`_. |
|---|
| 279 | | |
|---|
| 280 | | .. _generic views documentation: ../generic_views/ |
|---|
| 281 | | |
|---|
| 282 | | Coming soon |
|---|
| 283 | | =========== |
|---|
| 284 | | |
|---|
| 285 | | The tutorial ends here for the time being. But check back soon for the next |
|---|
| 286 | | installments: |
|---|
| 287 | | |
|---|
| 288 | | * Advanced form processing |
|---|
| 289 | | * Using the RSS framework |
|---|
| 290 | | * Using the cache framework |
|---|
| 291 | | * Using the comments framework |
|---|
| 292 | | * Advanced admin features: Permissions |
|---|
| 293 | | * Advanced admin features: Custom JavaScript |
|---|
| 294 | | |
|---|
| 295 | | In the meantime, you can read through the rest of the `Django documentation`_ |
|---|
| 296 | | and start writing your own applications. |
|---|
| 297 | | |
|---|
| 298 | | .. _Tutorial 3: ../tutorial03/ |
|---|
| 299 | | .. _Django documentation: http://www.djangoproject.com/documentation/ |
|---|
| | 255 | Uruchom teraz server, i użyj Twojej nowej aplikacji ankiet opartej o widoki generyczne. |
|---|
| | 256 | |
|---|
| | 257 | Aby dowiedzieć się więcej o widokach generycznych, zobacz `dokumentacje widoków generycznych`_. |
|---|
| | 258 | |
|---|
| | 259 | .. _dokumentacje widoków generycznych: http://www.djangoproject.com/documentation/generic_views/ |
|---|
| | 260 | |
|---|
| | 261 | Już w krótce |
|---|
| | 262 | ============ |
|---|
| | 263 | |
|---|
| | 264 | Ten tutorial kończy sie w tym momencie. Ale zobacz w krótce po nowe informacje: |
|---|
| | 265 | |
|---|
| | 266 | * Zawansowane przetwarzanie formularzy |
|---|
| | 267 | * Używanie framework'a RSS |
|---|
| | 268 | * Używanie framework'a cache'owania |
|---|
| | 269 | * Używanie framework'a komentarzy |
|---|
| | 270 | * Zaawansowane funckje admina: uprawnienia |
|---|
| | 271 | * Zaawansowane funckje admina: Własny Java script |
|---|
| | 272 | |
|---|
| | 273 | W między czasie możesz przeczytać resztę `dokumentacji Django`_ i zacząć pisać własne aplikacje. |
|---|
| | 274 | |
|---|
| | 275 | .. _tutorial'a nr 3: ../tutorial03/ |
|---|
| | 276 | .. _tutorial 3: ../tutorial03/ |
|---|
| | 277 | .. _dokumentacji Django: http://www.djangoproject.com/documentation/ |
|---|