Ticket #14255: mysite_refactor.diff

File mysite_refactor.diff, 11.3 KB (added by Adam Endicott, 14 years ago)
  • docs/intro/tutorial01.txt

     
    268268    configuration and apps for a particular Web site. A project can contain
    269269    multiple apps. An app can be in multiple projects.
    270270
    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.
     271Your apps can live anywhere on your `Python path`_. In this tutorial we will
     272create our poll app in the :file:`mysite` directory for simplicity.
    275273
    276274To create your app, make sure you're in the :file:`mysite` directory and type
    277275this command:
     
    369367    Django installation.
    370368
    371369Edit 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
    373371it'll look like this::
    374372
    375373    INSTALLED_APPS = (
     
    377375        'django.contrib.contenttypes',
    378376        'django.contrib.sessions',
    379377        'django.contrib.sites',
    380         'mysite.polls'
     378        'polls'
    381379    )
    382380
    383 Now Django knows ``mysite`` includes the ``polls`` app. Let's run another
     381Now Django knows to include the ``polls`` app. Let's run another
    384382command:
    385383
    386384.. code-block:: bash
     
    488486up the project's environment for you. "Setting up the environment" involves two
    489487things:
    490488
    491     * Putting ``mysite`` on ``sys.path``. For flexibility, several pieces of
     489    * Putting ``polls`` on ``sys.path``. For flexibility, several pieces of
    492490      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``
    494492      package has to be on ``sys.path``.
    495493
    496494      We've already seen one example of this: the :setting:`INSTALLED_APPS`
     
    502500.. admonition:: Bypassing manage.py
    503501
    504502    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``.
    508506
    509507    For more information on all of this, see the :doc:`django-admin.py
    510508    documentation </ref/django-admin>`.
    511509
    512510Once you're in the shell, explore the :doc:`database API </topics/db/queries>`::
    513511
    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.
    515513
    516514    # No polls are in the system yet.
    517515    >>> Poll.objects.all()
     
    619617Save these changes and start a new Python interactive shell by running
    620618``python manage.py shell`` again::
    621619
    622     >>> from mysite.polls.models import Poll, Choice
     620    >>> from polls.models import Poll, Choice
    623621
    624622    # Make sure our __unicode__() addition worked.
    625623    >>> Poll.objects.all()
  • docs/intro/tutorial02.txt

     
    103103objects have an admin interface. To do this, create a file called
    104104``admin.py`` in your ``polls`` directory, and edit it to look like this::
    105105
    106     from mysite.polls.models import Poll
     106    from polls.models import Poll
    107107    from django.contrib import admin
    108108
    109109    admin.site.register(Poll)
     
    239239There are two ways to solve this problem. The first is to register ``Choice``
    240240with the admin just as we did with ``Poll``. That's easy::
    241241
    242     from mysite.polls.models import Choice
     242    from polls.models import Choice
    243243
    244244    admin.site.register(Choice)
    245245
  • docs/intro/tutorial03.txt

     
    8484    admin.autodiscover()
    8585
    8686    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'),
    9191        (r'^admin/', include(admin.site.urls)),
    9292    )
    9393
     
    9696the :setting:`ROOT_URLCONF` setting. It finds the variable named ``urlpatterns``
    9797and traverses the regular expressions in order. When it finds a regular
    9898expression 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::
     99function ``detail()`` from ``polls/views.py``. Finally, it calls that
     100``detail()`` function like so::
    101101
    102102    detail(request=<HttpRequest object>, poll_id='23')
    103103
     
    112112-- unless you have a sick sense of humor, in which case you can do something
    113113like this::
    114114
    115     (r'^polls/latest\.php$', 'mysite.polls.views.index'),
     115    (r'^polls/latest\.php$', 'polls.views.index'),
    116116
    117117But, don't do that. It's silly.
    118118
     
    148148
    149149    ViewDoesNotExist at /polls/
    150150
    151     Tried index in module mysite.polls.views. Error was: 'module'
     151    Tried index in module polls.views. Error was: 'module'
    152152    object has no attribute 'index'
    153153
    154154This error happened because you haven't written a function ``index()`` in the
    155 module ``mysite/polls/views.py``.
     155module ``polls/views.py``.
    156156
    157157Try "/polls/23/", "/polls/23/results/" and "/polls/23/vote/". The error
    158158messages tell you which view Django tried (and failed to find, because you
     
    207207view, which displays the latest 5 poll questions in the system, separated by
    208208commas, according to publication date::
    209209
    210     from mysite.polls.models import Poll
     210    from polls.models import Poll
    211211    from django.http import HttpResponse
    212212
    213213    def index(request):
     
    220220So let's use Django's template system to separate the design from Python::
    221221
    222222    from django.template import Context, loader
    223     from mysite.polls.models import Poll
     223    from polls.models import Poll
    224224    from django.http import HttpResponse
    225225
    226226    def index(request):
     
    279279rewritten::
    280280
    281281    from django.shortcuts import render_to_response
    282     from mysite.polls.models import Poll
     282    from polls.models import Poll
    283283
    284284    def index(request):
    285285        latest_poll_list = Poll.objects.all().order_by('-pub_date')[:5]
     
    432432the URLconf, you may notice there's a fair bit of redundancy in it::
    433433
    434434    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'),
    439439    )
    440440
    441 Namely, ``mysite.polls.views`` is in every callback.
     441Namely, ``polls.views`` is in every callback.
    442442
    443443Because this is a common case, the URLconf framework provides a shortcut for
    444444common prefixes. You can factor out the common prefixes and add them as the
    445445first argument to :func:`~django.conf.urls.defaults.patterns`, like so::
    446446
    447     urlpatterns = patterns('mysite.polls.views',
     447    urlpatterns = patterns('polls.views',
    448448        (r'^polls/$', 'index'),
    449449        (r'^polls/(?P<poll_id>\d+)/$', 'detail'),
    450450        (r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
     
    476476
    477477    # ...
    478478    urlpatterns = patterns('',
    479         (r'^polls/', include('mysite.polls.urls')),
     479        (r'^polls/', include('polls.urls')),
    480480        # ...
    481481
    482482:func:`~django.conf.urls.defaults.include`, simply, references another URLconf.
     
    491491    * Django will find the match at ``'^polls/'``
    492492
    493493    * Then, Django will strip off the matching text (``"polls/"``) and send the
    494       remaining text -- ``"34/"`` -- to the 'mysite.polls.urls' URLconf for
     494      remaining text -- ``"34/"`` -- to the 'polls.urls' URLconf for
    495495      further processing.
    496496
    497 Now that we've decoupled that, we need to decouple the 'mysite.polls.urls'
     497Now that we've decoupled that, we need to decouple the 'polls.urls'
    498498URLconf by removing the leading "polls/" from each line, and removing the
    499499lines registering the admin site::
    500500
    501     urlpatterns = patterns('mysite.polls.views',
     501    urlpatterns = patterns('polls.views',
    502502        (r'^$', 'index'),
    503503        (r'^(?P<poll_id>\d+)/$', 'detail'),
    504504        (r'^(?P<poll_id>\d+)/results/$', 'results'),
  • docs/intro/tutorial04.txt

     
    8080    from django.http import HttpResponseRedirect, HttpResponse
    8181    from django.core.urlresolvers import reverse
    8282    from django.template import RequestContext
    83     from mysite.polls.models import Choice, Poll
     83    from polls.models import Choice, Poll
    8484    # ...
    8585    def vote(request, poll_id):
    8686        p = get_object_or_404(Poll, pk=poll_id)
     
    9898            # Always return an HttpResponseRedirect after successfully dealing
    9999            # with POST data. This prevents data from being posted twice if a
    100100            # 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,)))
    102102
    103103This code includes a few things we haven't covered yet in this tutorial:
    104104
     
    222222
    223223    from django.conf.urls.defaults import *
    224224
    225     urlpatterns = patterns('mysite.polls.views',
     225    urlpatterns = patterns('polls.views',
    226226        (r'^$', 'index'),
    227227        (r'^(?P<poll_id>\d+)/$', 'detail'),
    228228        (r'^(?P<poll_id>\d+)/results/$', 'results'),
     
    232232Change it like so::
    233233
    234234    from django.conf.urls.defaults import *
    235     from mysite.polls.models import Poll
     235    from polls.models import Poll
    236236
    237237    info_dict = {
    238238        'queryset': Poll.objects.all(),
     
    242242        (r'^$', 'django.views.generic.list_detail.object_list', info_dict),
    243243        (r'^(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', info_dict),
    244244        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'),
    246246    )
    247247
    248248We're using two generic views here:
Back to Top