選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

forms.py 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. from django import forms
  2. from django.db.models.loading import get_model
  3. from django.utils.translation import ugettext_lazy as _
  4. from oscar.core.validators import URLDoesNotExistValidator
  5. FlatPage = get_model('flatpages', 'FlatPage')
  6. class PageSearchForm(forms.Form):
  7. """
  8. Search form to filter pages by *title.
  9. """
  10. title = forms.CharField(required=False, label=_("Title"))
  11. class PageUpdateForm(forms.ModelForm):
  12. """
  13. Update form to create/update flatpages. It provides a *title*, *url*,
  14. and *content* field. The specified URL will be validated and check if
  15. the same URL already exists in the system.
  16. """
  17. url = forms.CharField(max_length=128, required=False, label=_("URL"),
  18. help_text=_("Example: '/about/contact/'."))
  19. def clean_url(self):
  20. """
  21. Validate the input for field *url* checking if the specified
  22. URL already exists. If it is an actual update and the URL has
  23. not been changed, validation will be skipped.
  24. Returns cleaned URL or raises an exception.
  25. """
  26. url = self.cleaned_data['url']
  27. if 'url' in self.changed_data:
  28. if not url.endswith('/'):
  29. url += '/'
  30. URLDoesNotExistValidator()(url)
  31. return url
  32. class Meta:
  33. model = FlatPage
  34. fields = ('title', 'url', 'content')