Ticket #14255: mysite_refactor.diff
File mysite_refactor.diff, 11.3 KB (added by , 14 years ago) |
---|
-
docs/intro/tutorial01.txt
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 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'``. So370 :setting:`INSTALLED_APPS` setting to include the string ``'polls'``. So 373 371 it'll look like this:: 374 372 375 373 INSTALLED_APPS = ( … … 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`` includesthe ``polls`` app. Let's run another381 Now Django knows to include the ``polls`` app. Let's run another 384 382 command: 385 383 386 384 .. code-block:: bash … … 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 of489 * 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 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) and506 set the ``DJANGO_SETTINGS_MODULE`` environment variable to507 ``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 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, Choice620 >>> from polls.models import Poll, Choice 623 621 624 622 # Make sure our __unicode__() addition worked. 625 623 >>> Poll.objects.all() -
docs/intro/tutorial02.txt
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 Poll106 from polls.models import Poll 107 107 from django.contrib import admin 108 108 109 109 admin.site.register(Poll) … … 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 Choice242 from polls.models import Choice 243 243 244 244 admin.site.register(Choice) 245 245 -
docs/intro/tutorial03.txt
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 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 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 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 … … 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 Poll210 from polls.models import Poll 211 211 from django.http import HttpResponse 212 212 213 213 def index(request): … … 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 Poll223 from polls.models import Poll 224 224 from django.http import HttpResponse 225 225 226 226 def index(request): … … 279 279 rewritten:: 280 280 281 281 from django.shortcuts import render_to_response 282 from mysite.polls.models import Poll282 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 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'), … … 476 476 477 477 # ... 478 478 urlpatterns = patterns('', 479 (r'^polls/', include(' mysite.polls.urls')),479 (r'^polls/', include('polls.urls')), 480 480 # ... 481 481 482 482 :func:`~django.conf.urls.defaults.include`, simply, references another URLconf. … … 491 491 * Django will find the match at ``'^polls/'`` 492 492 493 493 * Then, Django will strip off the matching text (``"polls/"``) and send the 494 remaining text -- ``"34/"`` -- to the ' mysite.polls.urls' URLconf for494 remaining text -- ``"34/"`` -- to the 'polls.urls' URLconf for 495 495 further processing. 496 496 497 Now that we've decoupled that, we need to decouple the ' mysite.polls.urls'497 Now that we've decoupled that, we need to decouple the 'polls.urls' 498 498 URLconf by removing the leading "polls/" from each line, and removing the 499 499 lines registering the admin site:: 500 500 501 urlpatterns = patterns(' mysite.polls.views',501 urlpatterns = patterns('polls.views', 502 502 (r'^$', 'index'), 503 503 (r'^(?P<poll_id>\d+)/$', 'detail'), 504 504 (r'^(?P<poll_id>\d+)/results/$', 'results'), -
docs/intro/tutorial04.txt
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, Poll83 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 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 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 232 Change it like so:: 233 233 234 234 from django.conf.urls.defaults import * 235 from mysite.polls.models import Poll235 from polls.models import Poll 236 236 237 237 info_dict = { 238 238 'queryset': Poll.objects.all(), … … 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: