From 93d83df61195ea598bc0a2a5cdce1df8e88fcb6d Mon Sep 17 00:00:00 2001 From: Boulder Sprinters Date: Fri, 15 Dec 2006 18:32:47 +0000 Subject: boulder-oracle-sprint: Merged to trunk [4210]. git-svn-id: http://code.djangoproject.com/svn/django/branches/boulder-oracle-sprint@4212 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- tests/modeltests/model_forms/__init__.py | 0 tests/modeltests/model_forms/models.py | 44 + tests/regressiontests/forms/tests.py | 1285 +++++++++++++++++++++++++++--- tests/regressiontests/templates/tests.py | 18 +- tests/runtests.py | 39 +- 5 files changed, 1250 insertions(+), 136 deletions(-) create mode 100644 tests/modeltests/model_forms/__init__.py create mode 100644 tests/modeltests/model_forms/models.py (limited to 'tests') diff --git a/tests/modeltests/model_forms/__init__.py b/tests/modeltests/model_forms/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/modeltests/model_forms/models.py b/tests/modeltests/model_forms/models.py new file mode 100644 index 0000000000..b51b4e1a8b --- /dev/null +++ b/tests/modeltests/model_forms/models.py @@ -0,0 +1,44 @@ +""" +34. Generating HTML forms from models + +Django provides shortcuts for creating Form objects from a model class. +""" + +from django.db import models + +class Category(models.Model): + name = models.CharField(maxlength=20) + url = models.CharField('The URL', maxlength=20) + + def __str__(self): + return self.name + +class Article(models.Model): + headline = models.CharField(maxlength=50) + pub_date = models.DateTimeField() + categories = models.ManyToManyField(Category) + + def __str__(self): + return self.headline + +__test__ = {'API_TESTS': """ +>>> from django.newforms import form_for_model +>>> CategoryForm = form_for_model(Category) +>>> f = CategoryForm() +>>> print f + + + +>>> print f.as_ul() +
  • +
  • +
  • +>>> print f['name'] + + +>>> f = CategoryForm(auto_id=False) +>>> print f.as_ul() +
  • ID:
  • +
  • Name:
  • +
  • The URL:
  • +"""} diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index c4dd7074a5..c75cedab14 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -4,6 +4,14 @@ r""" >>> import datetime >>> import re +########### +# Widgets # +########### + +Each Widget class corresponds to an HTML form widget. A Widget knows how to +render itself, given a field name and some data. Widgets don't perform +validation. + # TextInput Widget ############################################################ >>> w = TextInput() @@ -156,10 +164,18 @@ u' -as_textarea() and as_text() are shortcuts for changing the output widget type: +as_textarea(), as_text() and as_hidden() are shortcuts for changing the output +widget type: >>> f['subject'].as_textarea() u'' >>> f['message'].as_text() u'' +>>> f['message'].as_hidden() +u'' The 'widget' parameter to a Field can also be an instance: >>> class ContactForm(Form): ... subject = CharField() ... message = CharField(widget=Textarea(attrs={'rows': 80, 'cols': 20})) ->>> f = ContactForm() +>>> f = ContactForm(auto_id=False) >>> print f['message'] -Instance-level attrs are *not* carried over to as_textarea() and as_text(): +Instance-level attrs are *not* carried over to as_textarea(), as_text() and +as_hidden(): >>> f['message'].as_text() u'' ->>> f = ContactForm({'subject': 'Hello', 'message': 'I love you.'}) +>>> f = ContactForm({'subject': 'Hello', 'message': 'I love you.'}, auto_id=False) >>> f['subject'].as_textarea() u'' >>> f['message'].as_text() u'' +>>> f['message'].as_hidden() +u'' For a form with a ->>> f = FrameworkForm({'name': 'Django', 'language': 'P'}) +>>> f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False) >>> print f['language'] +Add widget=RadioSelect to use that widget with a ChoiceField. +>>> class FrameworkForm(Form): +... name = CharField() +... language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=RadioSelect) +>>> f = FrameworkForm(auto_id=False) +>>> print f['language'] + +>>> print f +Name: +Language: +>>> print f.as_ul() +
  • Name:
  • +
  • Language:
  • + +Regarding auto_id and