~ubuntu-branches/debian/squeeze/python-django/squeeze

« back to all changes in this revision

Viewing changes to docs/intro/tutorial02.txt

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb, Chris Lamb, David Spreen, Sandro Tosi
  • Date: 2008-11-19 21:31:00 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081119213100-gp0lqhxl1qxa6dgl
Tags: 1.0.2-1
[ Chris Lamb ]
* New upstream bugfix release. Closes: #505783
* Add myself to Uploaders with ACK from Brett.

[ David Spreen ]
* Remove python-pysqlite2 from Recommends because Python 2.5 includes
  sqlite library used by Django. Closes: 497886

[ Sandro Tosi ]
* debian/control
  - switch Vcs-Browser field to viewsvn

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
    Generating admin sites for your staff or clients to add, change and delete
14
14
    content is tedious work that doesn't require much creativity. For that
15
15
    reason, Django entirely automates creation of admin interfaces for models.
16
 
    
 
16
 
17
17
    Django was written in a newsroom environment, with a very clear separation
18
18
    between "content publishers" and the "public" site. Site managers use the
19
19
    system to add news stories, events, sports scores, etc., and that content is
20
20
    displayed on the public site. Django solves the problem of creating a
21
21
    unified interface for site administrators to edit content.
22
 
    
 
22
 
23
23
    The admin isn't necessarily intended to be used by site visitors; it's for
24
24
    site managers.
25
25
 
30
30
activate the admin site for your installation, do these three things:
31
31
 
32
32
    * Add ``"django.contrib.admin"`` to your :setting:`INSTALLED_APPS` setting.
33
 
    
 
33
 
34
34
    * Run ``python manage.py syncdb``. Since you have added a new application
35
35
      to :setting:`INSTALLED_APPS`, the database tables need to be updated.
36
 
      
 
36
 
37
37
    * Edit your ``mysite/urls.py`` file and uncomment the lines below the
38
 
      "Uncomment this for admin:" comments. This file is a URLconf; we'll dig
39
 
      into URLconfs in the next tutorial. For now, all you need to know is that
40
 
      it maps URL roots to applications. In the end, you should have a
41
 
      ``urls.py`` file that looks like this:
42
 
      
 
38
      "Uncomment the next two lines..." comment. This file is a URLconf;
 
39
      we'll dig into URLconfs in the next tutorial. For now, all you need to
 
40
      know is that it maps URL roots to applications. In the end, you should
 
41
      have a ``urls.py`` file that looks like this:
 
42
 
43
43
      .. parsed-literal::
44
 
      
 
44
 
45
45
          from django.conf.urls.defaults import *
46
46
 
47
47
          # Uncomment the next two lines to enable the admin:
59
59
              # Uncomment the next line to enable the admin:
60
60
              **(r'^admin/(.*)', admin.site.root),**
61
61
          )
62
 
          
 
62
 
63
63
      (The bold lines are the ones that needed to be uncommented.)
64
64
 
65
65
Start the development server
97
97
But where's our poll app? It's not displayed on the admin index page.
98
98
 
99
99
Just one thing to do: We need to tell the admin that ``Poll``
100
 
objects have an admin interface. Edit the ``mysite/polls/admin.py`` file and
101
 
add the following to the bottom of the file::
 
100
objects have an admin interface. To do this, create a file called
 
101
``admin.py`` in your ``polls`` directory, and edit it to look like this::
102
102
 
103
103
    from mysite.polls.models import Poll
104
104
    from django.contrib import admin
105
 
    
 
105
 
106
106
    admin.site.register(Poll)
107
107
 
108
 
Now reload the Django admin page to see your changes. Note that you don't have
109
 
to restart the development server -- the server will auto-reload your project,
110
 
so any modifications code will be seen immediately in your browser.
 
108
You'll need to restart the development server to see your changes. Normally,
 
109
the server auto-reloads code every time you modify a file, but the action of
 
110
creating a new file doesn't trigger the auto-reloading logic.
111
111
 
112
112
Explore the free admin functionality
113
113
====================================
133
133
Things to note here:
134
134
 
135
135
    * The form is automatically generated from the Poll model.
136
 
    
 
136
 
137
137
    * The different model field types (:class:`~django.db.models.DateTimeField`,
138
138
      :class:`~django.db.models.CharField`) correspond to the appropriate HTML
139
139
      input widget. Each type of field knows how to display itself in the Django
140
140
      admin.
141
 
    
 
141
 
142
142
    * Each :class:`~django.db.models.DateTimeField` gets free JavaScript
143
143
      shortcuts. Dates get a "Today" shortcut and calendar popup, and times get
144
144
      a "Now" shortcut and a convenient popup that lists commonly entered times.
147
147
 
148
148
    * Save -- Saves changes and returns to the change-list page for this type of
149
149
      object.
150
 
      
 
150
 
151
151
    * Save and continue editing -- Saves changes and reloads the admin page for
152
152
      this object.
153
 
      
 
153
 
154
154
    * Save and add another -- Saves changes and loads a new, blank form for this
155
155
      type of object.
156
 
      
 
156
 
157
157
    * Delete -- Displays a delete confirmation page.
158
158
 
159
159
Change the "Date published" by clicking the "Today" and "Now" shortcuts. Then
178
178
 
179
179
    class PollAdmin(admin.ModelAdmin):
180
180
        fields = ['pub_date', 'question']
181
 
        
 
181
 
182
182
    admin.site.register(Poll, PollAdmin)
183
183
 
184
184
You'll follow this pattern -- create a model admin object, then pass it as the
185
185
second argument to ``admin.site.register()`` -- any time you need to change the
186
186
admin options for an object.
187
 
    
 
187
 
188
188
This particular change above makes the "Publication date" come before the
189
189
"Question" field:
190
190
 
202
202
            (None,               {'fields': ['question']}),
203
203
            ('Date information', {'fields': ['pub_date']}),
204
204
        ]
205
 
        
 
205
 
206
206
    admin.site.register(Poll, PollAdmin)
207
207
 
208
208
The first element of each tuple in ``fieldsets`` is the title of the fieldset.
235
235
 
236
236
There are two ways to solve this problem. The first register ``Choice`` with the
237
237
admin just as we did with ``Poll``. That's easy::
238
 
    
 
238
 
239
239
    from mysite.polls.models import Choice
240
 
    
 
240
 
241
241
    admin.site.register(Choice)
242
242
 
243
243
Now "Choices" is an available option in the Django admin. The "Add choice" form
268
268
    class ChoiceInline(admin.StackedInline):
269
269
        model = Choice
270
270
        extra = 3
271
 
        
 
271
 
272
272
    class PollAdmin(admin.ModelAdmin):
273
273
        fieldsets = [
274
274
            (None,               {'fields': ['question']}),