Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add missing imports and models to the examples in the view layer docu…

…mentation
  • Loading branch information...
commit cd72c55d8603751af40a55d2d18f264827fa0744 1 parent e4591de
Silvan Spross authored May 18, 2013 frog32 committed May 18, 2013
3  docs/ref/template-response.txt
@@ -215,6 +215,7 @@ re-rendered, you can re-evaluate the rendered content, and assign
215 215
 the content of the response manually::
216 216
 
217 217
     # Set up a rendered TemplateResponse
  218
+    >>> from django.template.response import TemplateResponse
218 219
     >>> t = TemplateResponse(request, 'original.html', {})
219 220
     >>> t.render()
220 221
     >>> print(t.content)
@@ -256,6 +257,8 @@ To define a post-render callback, just define a function that takes
256 257
 a single argument -- response -- and register that function with
257 258
 the template response::
258 259
 
  260
+    from django.template.response import TemplateResponse
  261
+
259 262
     def my_render_callback(response):
260 263
         # Do content-sensitive processing
261 264
         do_post_processing()
6  docs/topics/class-based-views/generic-display.txt
@@ -248,7 +248,7 @@ specify the objects that the view will operate upon -- you can also
248 248
 specify the list of objects using the ``queryset`` argument::
249 249
 
250 250
     from django.views.generic import DetailView
251  
-    from books.models import Publisher, Book
  251
+    from books.models import Publisher
252 252
 
253 253
     class PublisherDetail(DetailView):
254 254
 
@@ -326,6 +326,7 @@ various useful things are stored on ``self``; as well as the request
326 326
 Here, we have a URLconf with a single captured group::
327 327
 
328 328
     # urls.py
  329
+    from django.conf.urls import patterns
329 330
     from books.views import PublisherBookList
330 331
 
331 332
     urlpatterns = patterns('',
@@ -375,6 +376,7 @@ Imagine we had a ``last_accessed`` field on our ``Author`` object that we were
375 376
 using to keep track of the last time anybody looked at that author::
376 377
 
377 378
     # models.py
  379
+    from django.db import models
378 380
 
379 381
     class Author(models.Model):
380 382
         salutation = models.CharField(max_length=10)
@@ -390,6 +392,7 @@ updated.
390 392
 First, we'd need to add an author detail bit in the URLconf to point to a
391 393
 custom view::
392 394
 
  395
+    from django.conf.urls import patterns, url
393 396
     from books.views import AuthorDetailView
394 397
 
395 398
     urlpatterns = patterns('',
@@ -401,7 +404,6 @@ Then we'd write our new view -- ``get_object`` is the method that retrieves the
401 404
 object -- so we simply override it and wrap the call::
402 405
 
403 406
     from django.views.generic import DetailView
404  
-    from django.shortcuts import get_object_or_404
405 407
     from django.utils import timezone
406 408
     from books.models import Author
407 409
 
1  docs/topics/class-based-views/generic-editing.txt
@@ -222,6 +222,7 @@ works for AJAX requests as well as 'normal' form POSTs::
222 222
 
223 223
     from django.http import HttpResponse
224 224
     from django.views.generic.edit import CreateView
  225
+    from myapp.models import Author
225 226
 
226 227
     class AjaxableResponseMixin(object):
227 228
         """
8  docs/topics/class-based-views/mixins.txt
@@ -258,6 +258,7 @@ mixin.
258 258
 We can hook this into our URLs easily enough::
259 259
 
260 260
     # urls.py
  261
+    from django.conf.urls import patterns, url
261 262
     from books.views import RecordInterest
262 263
 
263 264
     urlpatterns = patterns('',
@@ -440,6 +441,7 @@ Our new ``AuthorDetail`` looks like this::
440 441
     from django.core.urlresolvers import reverse
441 442
     from django.views.generic import DetailView
442 443
     from django.views.generic.edit import FormMixin
  444
+    from books.models import Author
443 445
 
444 446
     class AuthorInterestForm(forms.Form):
445 447
         message = forms.CharField()
@@ -546,6 +548,8 @@ template as ``AuthorDisplay`` is using on ``GET``.
546 548
 
547 549
 .. code-block:: python
548 550
 
  551
+    from django.core.urlresolvers import reverse
  552
+    from django.http import HttpResponseForbidden
549 553
     from django.views.generic import FormView
550 554
     from django.views.generic.detail import SingleObjectMixin
551 555
 
@@ -657,6 +661,8 @@ own version of :class:`~django.views.generic.detail.DetailView` by mixing
657 661
 :class:`~django.views.generic.detail.DetailView` before template
658 662
 rendering behavior has been mixed in)::
659 663
 
  664
+    from django.views.generic.detail import BaseDetailView
  665
+
660 666
     class JSONDetailView(JSONResponseMixin, BaseDetailView):
661 667
         pass
662 668
 
@@ -675,6 +681,8 @@ and override the implementation of
675 681
 to defer to the appropriate subclass depending on the type of response that the
676 682
 user requested::
677 683
 
  684
+    from django.views.generic.detail import SingleObjectTemplateResponseMixin
  685
+
678 686
     class HybridDetailView(JSONResponseMixin, SingleObjectTemplateResponseMixin, BaseDetailView):
679 687
         def render_to_response(self, context):
680 688
             # Look for a 'format=json' GET argument
2  docs/topics/files.txt
@@ -27,6 +27,8 @@ to deal with that file.
27 27
 Consider the following model, using an :class:`~django.db.models.ImageField` to
28 28
 store a photo::
29 29
 
  30
+    from django.db import models
  31
+
30 32
     class Car(models.Model):
31 33
         name = models.CharField(max_length=255)
32 34
         price = models.DecimalField(max_digits=5, decimal_places=2)
2  docs/topics/http/file-uploads.txt
@@ -15,6 +15,7 @@ Basic file uploads
15 15
 
16 16
 Consider a simple form containing a :class:`~django.forms.FileField`::
17 17
 
  18
+    # In forms.py...
18 19
     from django import forms
19 20
 
20 21
     class UploadFileForm(forms.Form):
@@ -39,6 +40,7 @@ something like::
39 40
 
40 41
     from django.http import HttpResponseRedirect
41 42
     from django.shortcuts import render_to_response
  43
+    from .forms import UploadFileForm
42 44
 
43 45
     # Imaginary function to handle an uploaded file.
44 46
     from somewhere import handle_uploaded_file
31  docs/topics/http/urls.txt
@@ -123,6 +123,8 @@ is ``(?P<name>pattern)``, where ``name`` is the name of the group and
123 123
 
124 124
 Here's the above example URLconf, rewritten to use named groups::
125 125
 
  126
+    from django.conf.urls import patterns, url
  127
+
126 128
     urlpatterns = patterns('',
127 129
         url(r'^articles/2003/$', 'news.views.special_case_2003'),
128 130
         url(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive'),
@@ -192,6 +194,8 @@ A convenient trick is to specify default parameters for your views' arguments.
192 194
 Here's an example URLconf and view::
193 195
 
194 196
     # URLconf
  197
+    from django.conf.urls import patterns, url
  198
+
195 199
     urlpatterns = patterns('',
196 200
         url(r'^blog/$', 'blog.views.page'),
197 201
         url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
@@ -370,11 +374,15 @@ An included URLconf receives any captured parameters from parent URLconfs, so
370 374
 the following example is valid::
371 375
 
372 376
     # In settings/urls/main.py
  377
+    from django.conf.urls import include, patterns, url
  378
+
373 379
     urlpatterns = patterns('',
374 380
         url(r'^(?P<username>\w+)/blog/', include('foo.urls.blog')),
375 381
     )
376 382
 
377 383
     # In foo/urls/blog.py
  384
+    from django.conf.urls import patterns, url
  385
+
378 386
     urlpatterns = patterns('foo.views',
379 387
         url(r'^$', 'blog.index'),
380 388
         url(r'^archive/$', 'blog.archive'),
@@ -397,6 +405,8 @@ function.
397 405
 
398 406
 For example::
399 407
 
  408
+    from django.conf.urls import patterns, url
  409
+
400 410
     urlpatterns = patterns('blog.views',
401 411
         url(r'^blog/(?P<year>\d{4})/$', 'year_archive', {'foo': 'bar'}),
402 412
     )
@@ -427,11 +437,15 @@ For example, these two URLconf sets are functionally identical:
427 437
 Set one::
428 438
 
429 439
     # main.py
  440
+    from django.conf.urls import include, patterns, url
  441
+
430 442
     urlpatterns = patterns('',
431 443
         url(r'^blog/', include('inner'), {'blogid': 3}),
432 444
     )
433 445
 
434 446
     # inner.py
  447
+    from django.conf.urls import patterns, url
  448
+
435 449
     urlpatterns = patterns('',
436 450
         url(r'^archive/$', 'mysite.views.archive'),
437 451
         url(r'^about/$', 'mysite.views.about'),
@@ -440,11 +454,15 @@ Set one::
440 454
 Set two::
441 455
 
442 456
     # main.py
  457
+    from django.conf.urls import include, patterns, url
  458
+
443 459
     urlpatterns = patterns('',
444 460
         url(r'^blog/', include('inner')),
445 461
     )
446 462
 
447 463
     # inner.py
  464
+    from django.conf.urls import patterns, url
  465
+
448 466
     urlpatterns = patterns('',
449 467
         url(r'^archive/$', 'mysite.views.archive', {'blogid': 3}),
450 468
         url(r'^about/$', 'mysite.views.about', {'blogid': 3}),
@@ -464,6 +482,8 @@ supported -- you can pass any callable object as the view.
464 482
 
465 483
 For example, given this URLconf in "string" notation::
466 484
 
  485
+    from django.conf.urls import patterns, url
  486
+
467 487
     urlpatterns = patterns('',
468 488
         url(r'^archive/$', 'mysite.views.archive'),
469 489
         url(r'^about/$', 'mysite.views.about'),
@@ -473,6 +493,7 @@ For example, given this URLconf in "string" notation::
473 493
 You can accomplish the same thing by passing objects rather than strings. Just
474 494
 be sure to import the objects::
475 495
 
  496
+    from django.conf.urls import patterns, url
476 497
     from mysite.views import archive, about, contact
477 498
 
478 499
     urlpatterns = patterns('',
@@ -485,6 +506,7 @@ The following example is functionally identical. It's just a bit more compact
485 506
 because it imports the module that contains the views, rather than importing
486 507
 each view individually::
487 508
 
  509
+    from django.conf.urls import patterns, url
488 510
     from mysite import views
489 511
 
490 512
     urlpatterns = patterns('',
@@ -501,6 +523,7 @@ the view prefix (as explained in "The view prefix" above) will have no effect.
501 523
 Note that :doc:`class based views</topics/class-based-views/index>` must be
502 524
 imported::
503 525
 
  526
+    from django.conf.urls import patterns, url
504 527
     from mysite.views import ClassBasedView
505 528
 
506 529
     urlpatterns = patterns('',
@@ -612,6 +635,9 @@ It's fairly common to use the same view function in multiple URL patterns in
612 635
 your URLconf. For example, these two URL patterns both point to the ``archive``
613 636
 view::
614 637
 
  638
+    from django.conf.urls import patterns, url
  639
+    from mysite.views import archive
  640
+
615 641
     urlpatterns = patterns('',
616 642
         url(r'^archive/(\d{4})/$', archive),
617 643
         url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}),
@@ -630,6 +656,9 @@ matching.
630 656
 
631 657
 Here's the above example, rewritten to use named URL patterns::
632 658
 
  659
+    from django.conf.urls import patterns, url
  660
+    from mysite.views import archive
  661
+
633 662
     urlpatterns = patterns('',
634 663
         url(r'^archive/(\d{4})/$', archive, name="full-archive"),
635 664
         url(r'^archive-summary/(\d{4})/$', archive, {'summary': True}, name="arch-summary"),
@@ -803,6 +832,8 @@ However, you can also ``include()`` a 3-tuple containing::
803 832
 
804 833
 For example::
805 834
 
  835
+    from django.conf.urls import include, patterns, url
  836
+
806 837
     help_patterns = patterns('',
807 838
         url(r'^basic/$', 'apps.help.views.views.basic'),
808 839
         url(r'^advanced/$', 'apps.help.views.views.advanced'),
6  docs/topics/http/views.txt
@@ -70,6 +70,8 @@ documentation.  Just return an instance of one of those subclasses instead of
70 70
 a normal :class:`~django.http.HttpResponse` in order to signify an error. For
71 71
 example::
72 72
 
  73
+    from django.http import HttpResponse, HttpResponseNotFound
  74
+
73 75
     def my_view(request):
74 76
         # ...
75 77
         if foo:
@@ -83,6 +85,8 @@ the :class:`~django.http.HttpResponse` documentation, you can also pass the
83 85
 HTTP status code into the constructor for :class:`~django.http.HttpResponse`
84 86
 to create a return class for any status code you like. For example::
85 87
 
  88
+    from django.http import HttpResponse
  89
+
86 90
     def my_view(request):
87 91
         # ...
88 92
 
@@ -110,6 +114,8 @@ standard error page for your application, along with an HTTP error code 404.
110 114
 Example usage::
111 115
 
112 116
     from django.http import Http404
  117
+    from django.shortcuts import render_to_response
  118
+    from polls.models import Poll
113 119
 
114 120
     def detail(request, poll_id):
115 121
         try:

0 notes on commit cd72c55

Please sign in to comment.
Something went wrong with that request. Please try again.