Changeset 2765
- Timestamp:
- 04/28/06 00:00:42 (2 years ago)
- Files:
-
- django/branches/magic-removal/docs/forms.txt (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/magic-removal/docs/forms.txt
r2721 r2765 10 10 11 11 If all you want to do is present forms for a user to create and/or 12 update a given object, don't read any further. Instead, click thyself 13 to the `generic views`_ documentation. The following exercises are 14 for those interested in how Django's form framework works and those 15 needing to do more than simple creation/updating. 12 update a given object, you may be able to use `generic views`_. 16 13 17 14 We'll take a top-down approach to examining Django's form validation framework, … … 33 30 zip_code = meta.CharField(maxlength=5, blank=True) 34 31 place_type = meta.IntegerField(choices=PLACE_TYPES) 35 class Meta: 36 admin = meta.Admin() 37 38 def __repr__(self): 32 33 class Admin: 34 pass 35 36 def __str__(self): 39 37 return self.name 40 38 41 Defining the above class is enough to create an admin interface to a `` place``,39 Defining the above class is enough to create an admin interface to a ``Place``, 42 40 but what if you want to allow public users to submit places? 43 41 … … 54 52 created when you define a new class:: 55 53 56 >>> from django.models.places import places57 >>> places.AddManipulator58 <class django.models.places.PlaceManipulatorAdd at 0x4c1540>59 >>> places.ChangeManipulator60 <class django.models.places.PlaceManipulatorChange at 0x4c1630>54 >>> from mysite.myapp.models import Place 55 >>> Place.AddManipulator 56 <class Place.ManipulatorAdd at 0x4c1540> 57 >>> Place.ChangeManipulator 58 <class Place.ManipulatorChange at 0x4c1630> 61 59 62 60 Using the ``AddManipulator`` … … 68 66 from django.shortcuts import render_to_response 69 67 from django.http import Http404, HttpResponse, HttpResponseRedirect 70 from django.models.places import places68 from mysite.myapp.models import Place 71 69 from django import forms 72 70 … … 74 72 """A naive approach to creating places; don't actually use this!""" 75 73 # Create the AddManipulator. 76 manipulator = places.AddManipulator()74 manipulator = Place.AddManipulator() 77 75 78 76 # Make a copy of the POSTed data so that do_html2python can … … 110 108 # Create a FormWrapper object that the template can use. Ignore 111 109 # the last two arguments to FormWrapper for now. 112 form = forms.FormWrapper( places.AddManipulator(), {}, {})110 form = forms.FormWrapper(Place.AddManipulator(), {}, {}) 113 111 return render_to_response('places/naive_create_form.html', {'form': form}) 114 112 … … 117 115 118 116 The ``forms.FormWrapper`` object is a wrapper that templates can 119 easily deal with to create forms. Here's the ``naive_create_form`` template:: 120 121 {% extends "base" %} 117 easily deal with to create forms. Here's the ``naive_create_form.html`` 118 template:: 119 120 {% extends "base.html" %} 122 121 123 122 {% block content %} … … 156 155 157 156 def create_place_with_validation(request): 158 manipulator = places.AddManipulator()157 manipulator = Place.AddManipulator() 159 158 new_data = request.POST.copy() 160 159 … … 172 171 on an error page (templated, of course):: 173 172 174 {% extends "base " %}173 {% extends "base.html" %} 175 174 176 175 {% block content %} … … 209 208 210 209 def create_place(request): 211 manipulator = places.AddManipulator()210 manipulator = Place.AddManipulator() 212 211 213 212 if request.POST: … … 238 237 and here's the ``create_form`` template:: 239 238 240 {% extends "base " %}239 {% extends "base.html" %} 241 240 242 241 {% block content %} … … 301 300 # ChangeManipulator at the same time. 302 301 try: 303 manipulator = places.ChangeManipulator(place_id)304 except places.PlaceDoesNotExist:302 manipulator = Place.ChangeManipulator(place_id) 303 except Place.DoesNotExist: 305 304 raise Http404 306 305
