strip_tags in django admin issues

116 views
Skip to first unread message

ChrisL

unread,
Jan 6, 2009, 12:14:34 PM1/6/09
to Django users
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!


Alex Koshelev

unread,
Jan 6, 2009, 1:01:16 PM1/6/09
to django...@googlegroups.com
Form class really doesn't have `strip_tags` method. I think you are
interested in `strip_tags` functon from `django.utils.html`(and your
comment says the same). So your method may look like this:

def clean_title(self):
from django.utils.html import strip_tags
title = strip_tags(self.cleaned_data["title"])
return title

Karen Tracey

unread,
Jan 6, 2009, 1:04:51 PM1/6/09
to django...@googlegroups.com

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

ChrisL

unread,
Jan 7, 2009, 4:46:08 AM1/7/09
to Django users
Alex - you are a legend. That's functioning perfectly. I've moved the
import line to the start of the file, but other than that used your
code and it's working a treat.

Thanks for the swift help Alex, and also Karen: together you've
clarified Django very clearly for me.

On Jan 6, 6:01 pm, "Alex Koshelev" <daeva...@gmail.com> wrote:
> Form class really doesn't have `strip_tags` method. I think you are
> interested in `strip_tags` functon from `django.utils.html`(and your
> comment says the same). So your method may look like this:
>
>         def clean_title(self):
>                from django.utils.html importstrip_tags
>                title =strip_tags(self.cleaned_data["title"])
>                return title
>
> On Tue, Jan 6, 2009 at 8: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 importstrip_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, usingstrip_tags(import from django.utils.html
Reply all
Reply to author
Forward
0 new messages