Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #14255 -- factor project name out of app imports in tutorial. T…

…hanks to adamend for the report and initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14066 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d81faf356c53c0cf099d48551e1d3982135f2db9 1 parent 307e71a
Simon Meers authored
26  docs/intro/tutorial01.txt
@@ -268,10 +268,8 @@ so you can focus on writing code rather than creating directories.
268 268
     configuration and apps for a particular Web site. A project can contain
269 269
     multiple apps. An app can be in multiple projects.
270 270
 
271  
-In this tutorial, we'll create our poll app in the :file:`mysite` directory,
272  
-for simplicity. As a consequence, the app will be coupled to the project --
273  
-that is, Python code within the poll app will refer to ``mysite.polls``.
274  
-Later in this tutorial, we'll discuss decoupling your apps for distribution.
  271
+Your apps can live anywhere on your `Python path`_. In this tutorial we will
  272
+create our poll app in the :file:`mysite` directory for simplicity.
275 273
 
276 274
 To create your app, make sure you're in the :file:`mysite` directory and type
277 275
 this command:
@@ -369,7 +367,7 @@ But first we need to tell our project that the ``polls`` app is installed.
369 367
     Django installation.
370 368
 
371 369
 Edit the :file:`settings.py` file again, and change the
372  
-:setting:`INSTALLED_APPS` setting to include the string ``'mysite.polls'``. So
  370
+:setting:`INSTALLED_APPS` setting to include the string ``'polls'``. So
373 371
 it'll look like this::
374 372
 
375 373
     INSTALLED_APPS = (
@@ -377,10 +375,10 @@ it'll look like this::
377 375
         'django.contrib.contenttypes',
378 376
         'django.contrib.sessions',
379 377
         'django.contrib.sites',
380  
-        'mysite.polls'
  378
+        'polls'
381 379
     )
382 380
 
383  
-Now Django knows ``mysite`` includes the ``polls`` app. Let's run another
  381
+Now Django knows to include the ``polls`` app. Let's run another
384 382
 command:
385 383
 
386 384
 .. code-block:: bash
@@ -488,9 +486,9 @@ We're using this instead of simply typing "python", because ``manage.py`` sets
488 486
 up the project's environment for you. "Setting up the environment" involves two
489 487
 things:
490 488
 
491  
-    * Putting ``mysite`` on ``sys.path``. For flexibility, several pieces of
  489
+    * Putting ``polls`` on ``sys.path``. For flexibility, several pieces of
492 490
       Django refer to projects in Python dotted-path notation (e.g.
493  
-      ``'mysite.polls.models'``). In order for this to work, the ``mysite``
  491
+      ``'polls.models'``). In order for this to work, the ``polls``
494 492
       package has to be on ``sys.path``.
495 493
 
496 494
       We've already seen one example of this: the :setting:`INSTALLED_APPS`
@@ -502,16 +500,16 @@ things:
502 500
 .. admonition:: Bypassing manage.py
503 501
 
504 502
     If you'd rather not use ``manage.py``, no problem. Just make sure ``mysite``
505  
-    is at the root level on the Python path (i.e., ``import mysite`` works) and
506  
-    set the ``DJANGO_SETTINGS_MODULE`` environment variable to
507  
-    ``mysite.settings``.
  503
+    and ``polls`` are at the root level on the Python path (i.e., ``import mysite``
  504
+    and ``import polls`` work) and set the ``DJANGO_SETTINGS_MODULE`` environment
  505
+    variable to ``mysite.settings``.
508 506
 
509 507
     For more information on all of this, see the :doc:`django-admin.py
510 508
     documentation </ref/django-admin>`.
511 509
 
512 510
 Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
513 511
 
514  
-    >>> from mysite.polls.models import Poll, Choice # Import the model classes we just wrote.
  512
+    >>> from polls.models import Poll, Choice # Import the model classes we just wrote.
515 513
 
516 514
     # No polls are in the system yet.
517 515
     >>> Poll.objects.all()
@@ -619,7 +617,7 @@ Note the addition of ``import datetime`` to reference Python's standard
619 617
 Save these changes and start a new Python interactive shell by running
620 618
 ``python manage.py shell`` again::
621 619
 
622  
-    >>> from mysite.polls.models import Poll, Choice
  620
+    >>> from polls.models import Poll, Choice
623 621
 
624 622
     # Make sure our __unicode__() addition worked.
625 623
     >>> Poll.objects.all()
4  docs/intro/tutorial02.txt
@@ -103,7 +103,7 @@ Just one thing to do: We need to tell the admin that ``Poll``
103 103
 objects have an admin interface. To do this, create a file called
104 104
 ``admin.py`` in your ``polls`` directory, and edit it to look like this::
105 105
 
106  
-    from mysite.polls.models import Poll
  106
+    from polls.models import Poll
107 107
     from django.contrib import admin
108 108
 
109 109
     admin.site.register(Poll)
@@ -239,7 +239,7 @@ Yet.
239 239
 There are two ways to solve this problem. The first is to register ``Choice``
240 240
 with the admin just as we did with ``Poll``. That's easy::
241 241
 
242  
-    from mysite.polls.models import Choice
  242
+    from polls.models import Choice
243 243
 
244 244
     admin.site.register(Choice)
245 245
 
48  docs/intro/tutorial03.txt
@@ -84,10 +84,10 @@ Time for an example. Edit ``mysite/urls.py`` so it looks like this::
84 84
     admin.autodiscover()
85 85
 
86 86
     urlpatterns = patterns('',
87  
-        (r'^polls/$', 'mysite.polls.views.index'),
88  
-        (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
89  
-        (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
90  
-        (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
  87
+        (r'^polls/$', 'polls.views.index'),
  88
+        (r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
  89
+        (r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
  90
+        (r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
91 91
         (r'^admin/', include(admin.site.urls)),
92 92
     )
93 93
 
@@ -96,8 +96,8 @@ This is worth a review. When somebody requests a page from your Web site -- say,
96 96
 the :setting:`ROOT_URLCONF` setting. It finds the variable named ``urlpatterns``
97 97
 and traverses the regular expressions in order. When it finds a regular
98 98
 expression that matches -- ``r'^polls/(?P<poll_id>\d+)/$'`` -- it loads the
99  
-function ``detail()`` from ``mysite/polls/views.py``. Finally,
100  
-it calls that ``detail()`` function like so::
  99
+function ``detail()`` from ``polls/views.py``. Finally, it calls that
  100
+``detail()`` function like so::
101 101
 
102 102
     detail(request=<HttpRequest object>, poll_id='23')
103 103
 
@@ -112,7 +112,7 @@ what you can do with them. And there's no need to add URL cruft such as ``.php``
112 112
 -- unless you have a sick sense of humor, in which case you can do something
113 113
 like this::
114 114
 
115  
-    (r'^polls/latest\.php$', 'mysite.polls.views.index'),
  115
+    (r'^polls/latest\.php$', 'polls.views.index'),
116 116
 
117 117
 But, don't do that. It's silly.
118 118
 
@@ -148,17 +148,17 @@ You should get a pleasantly-colored error page with the following message::
148 148
 
149 149
     ViewDoesNotExist at /polls/
150 150
 
151  
-    Tried index in module mysite.polls.views. Error was: 'module'
  151
+    Tried index in module polls.views. Error was: 'module'
152 152
     object has no attribute 'index'
153 153
 
154 154
 This error happened because you haven't written a function ``index()`` in the
155  
-module ``mysite/polls/views.py``.
  155
+module ``polls/views.py``.
156 156
 
157 157
 Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error
158 158
 messages tell you which view Django tried (and failed to find, because you
159 159
 haven't written any views yet).
160 160
 
161  
-Time to write the first view. Open the file ``mysite/polls/views.py``
  161
+Time to write the first view. Open the file ``polls/views.py``
162 162
 and put the following Python code in it::
163 163
 
164 164
     from django.http import HttpResponse
@@ -207,7 +207,7 @@ in :doc:`Tutorial 1 </intro/tutorial01>`. Here's one stab at the ``index()``
207 207
 view, which displays the latest 5 poll questions in the system, separated by
208 208
 commas, according to publication date::
209 209
 
210  
-    from mysite.polls.models import Poll
  210
+    from polls.models import Poll
211 211
     from django.http import HttpResponse
212 212
 
213 213
     def index(request):
@@ -220,7 +220,7 @@ you want to change the way the page looks, you'll have to edit this Python code.
220 220
 So let's use Django's template system to separate the design from Python::
221 221
 
222 222
     from django.template import Context, loader
223  
-    from mysite.polls.models import Poll
  223
+    from polls.models import Poll
224 224
     from django.http import HttpResponse
225 225
 
226 226
     def index(request):
@@ -279,7 +279,7 @@ template. Django provides a shortcut. Here's the full ``index()`` view,
279 279
 rewritten::
280 280
 
281 281
     from django.shortcuts import render_to_response
282  
-    from mysite.polls.models import Poll
  282
+    from polls.models import Poll
283 283
 
284 284
     def index(request):
285 285
         latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
@@ -432,19 +432,19 @@ Take some time to play around with the views and template system. As you edit
432 432
 the URLconf, you may notice there's a fair bit of redundancy in it::
433 433
 
434 434
     urlpatterns = patterns('',
435  
-        (r'^polls/$', 'mysite.polls.views.index'),
436  
-        (r'^polls/(?P<poll_id>\d+)/$', 'mysite.polls.views.detail'),
437  
-        (r'^polls/(?P<poll_id>\d+)/results/$', 'mysite.polls.views.results'),
438  
-        (r'^polls/(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
  435
+        (r'^polls/$', 'polls.views.index'),
  436
+        (r'^polls/(?P<poll_id>\d+)/$', 'polls.views.detail'),
  437
+        (r'^polls/(?P<poll_id>\d+)/results/$', 'polls.views.results'),
  438
+        (r'^polls/(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
439 439
     )
440 440
 
441  
-Namely, ``mysite.polls.views`` is in every callback.
  441
+Namely, ``polls.views`` is in every callback.
442 442
 
443 443
 Because this is a common case, the URLconf framework provides a shortcut for
444 444
 common prefixes. You can factor out the common prefixes and add them as the
445 445
 first argument to :func:`~django.conf.urls.defaults.patterns`, like so::
446 446
 
447  
-    urlpatterns = patterns('mysite.polls.views',
  447
+    urlpatterns = patterns('polls.views',
448 448
         (r'^polls/$', 'index'),
449 449
         (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
450 450
         (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
@@ -470,7 +470,7 @@ We've been editing the URLs in ``mysite/urls.py``, but the URL design of an
470 470
 app is specific to the app, not to the Django installation -- so let's move the
471 471
 URLs within the app directory.
472 472
 
473  
-Copy the file ``mysite/urls.py`` to ``mysite/polls/urls.py``. Then, change
  473
+Copy the file ``mysite/urls.py`` to ``polls/urls.py``. Then, change
474 474
 ``mysite/urls.py`` to remove the poll-specific URLs and insert an
475 475
 :func:`~django.conf.urls.defaults.include`::
476 476
 
@@ -479,7 +479,7 @@ Copy the file ``mysite/urls.py`` to ``mysite/polls/urls.py``. Then, change
479 479
 
480 480
     # ...
481 481
     urlpatterns = patterns('',
482  
-        (r'^polls/', include('mysite.polls.urls')),
  482
+        (r'^polls/', include('polls.urls')),
483 483
         # ...
484 484
     )
485 485
 
@@ -495,14 +495,14 @@ Here's what happens if a user goes to "/polls/34/" in this system:
495 495
     * Django will find the match at ``'^polls/'``
496 496
 
497 497
     * Then, Django will strip off the matching text (``"polls/"``) and send the
498  
-      remaining text -- ``"34/"`` -- to the 'mysite.polls.urls' URLconf for
  498
+      remaining text -- ``"34/"`` -- to the 'polls.urls' URLconf for
499 499
       further processing.
500 500
 
501  
-Now that we've decoupled that, we need to decouple the 'mysite.polls.urls'
  501
+Now that we've decoupled that, we need to decouple the 'polls.urls'
502 502
 URLconf by removing the leading "polls/" from each line, and removing the
503 503
 lines registering the admin site::
504 504
 
505  
-    urlpatterns = patterns('mysite.polls.views',
  505
+    urlpatterns = patterns('polls.views',
506 506
         (r'^$', 'index'),
507 507
         (r'^(?P<poll_id>\d+)/$', 'detail'),
508 508
         (r'^(?P<poll_id>\d+)/results/$', 'results'),
12  docs/intro/tutorial04.txt
@@ -74,13 +74,13 @@ created a URLconf for the polls application that includes this line::
74 74
     (r'^(?P<poll_id>\d+)/vote/$', 'vote'),
75 75
 
76 76
 We also created a dummy implementation of the ``vote()`` function. Let's
77  
-create a real version. Add the following to ``mysite/polls/views.py``::
  77
+create a real version. Add the following to ``polls/views.py``::
78 78
 
79 79
     from django.shortcuts import get_object_or_404, render_to_response
80 80
     from django.http import HttpResponseRedirect, HttpResponse
81 81
     from django.core.urlresolvers import reverse
82 82
     from django.template import RequestContext
83  
-    from mysite.polls.models import Choice, Poll
  83
+    from polls.models import Choice, Poll
84 84
     # ...
85 85
     def vote(request, poll_id):
86 86
         p = get_object_or_404(Poll, pk=poll_id)
@@ -98,7 +98,7 @@ create a real version. Add the following to ``mysite/polls/views.py``::
98 98
             # Always return an HttpResponseRedirect after successfully dealing
99 99
             # with POST data. This prevents data from being posted twice if a
100 100
             # user hits the Back button.
101  
-            return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(p.id,)))
  101
+            return HttpResponseRedirect(reverse('polls.views.results', args=(p.id,)))
102 102
 
103 103
 This code includes a few things we haven't covered yet in this tutorial:
104 104
 
@@ -222,7 +222,7 @@ tutorial so far::
222 222
 
223 223
     from django.conf.urls.defaults import *
224 224
 
225  
-    urlpatterns = patterns('mysite.polls.views',
  225
+    urlpatterns = patterns('polls.views',
226 226
         (r'^$', 'index'),
227 227
         (r'^(?P<poll_id>\d+)/$', 'detail'),
228 228
         (r'^(?P<poll_id>\d+)/results/$', 'results'),
@@ -232,7 +232,7 @@ tutorial so far::
232 232
 Change it like so::
233 233
 
234 234
     from django.conf.urls.defaults import *
235  
-    from mysite.polls.models import Poll
  235
+    from polls.models import Poll
236 236
 
237 237
     info_dict = {
238 238
         'queryset': Poll.objects.all(),
@@ -242,7 +242,7 @@ Change it like so::
242 242
         (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
243 243
         (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
244 244
         url(r'^(?P<object_id>\d+)/results/$', 'django.views.generic.list_detail.object_detail', dict(info_dict, template_name='polls/results.html'), 'poll_results'),
245  
-        (r'^(?P<poll_id>\d+)/vote/$', 'mysite.polls.views.vote'),
  245
+        (r'^(?P<poll_id>\d+)/vote/$', 'polls.views.vote'),
246 246
     )
247 247
 
248 248
 We're using two generic views here:

0 notes on commit d81faf3

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