On Tue, Jan 6, 2009 at 12:14 PM, ChrisL <10000angryc...@googlemail.com>wrote:

>
> Hi everyone,
>
> Been bashing against this one for a while now and am keen for
> suggestions. I'm trying to apply custom validation to forms within
> Django's default admin. First up, I want to strip out all html from a
> Title field. Following the official documentation I have developed
> this:
>
> ----------------------------
>
> from django.contrib import admin
> from django import forms
> from django.utils.html import strip_tags
> from queryclick.qc_news.models import *
>
> #Custom Data Validation in the Admin Interface
> class MyArticleAdminForm(forms.ModelForm):
>        class Meta:
>                model = Article
>
>        def clean_title(self):
>                #Remove all HTML, using strip_tags (import from
> django.utils.html
>                title = self.strip_tags('title')
>                #Always return cleaned data
>                return self.cleaned_data["title"]
>
> class ArticleAdmin(admin.ModelAdmin):
>        list_display = ('title', 'client', 'submitted_date', 'edited_date',
> 'go_live_date')
>        list_filter = ('client', 'submitted_date', 'edited_date',
> 'go_live_date')
>        ordering = ('-submitted_date',)
>        search_fields = ('title','client',)
>        form = MyArticleAdminForm
>
> admin.site.register(Copywriter)
> admin.site.register(Client)
> admin.site.register(Article, ArticleAdmin)
>
> ----------------------------
>
> But get an Attribute Error: 'ArticleForm' object has no attribute
> 'strip_tags' from line 13:
>
> title = self.strip_tags('title')
>
> Any and all suggestions warmly welcomed!
>
>
You could try:

return strip_tags(self.cleaned_data['title'])

though I have no knowledge of what that function does so I'm just assuming
it does what you want.

There are several things wrong with what you have: first, stirp_tags is a
simple function, not an object method, and not a method of ArticleForm
(hence the error when you try to call it via self.strip_tags).  Passing in
'title' will cause it to operate on the literal string 'title', whereas if
you want it to operate on the data that has been posted in the form, you
need to pass in self.cleaned_data['title'].  You assign what it turns to the
variable title but then you go ahead and return the original
self.cleaned_data['title'] that you have not modified, so even if you did
not get the error you are seeing, your clean function as written would
simply return what had been included in the form.

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to