Sunday 15 May 2011

python - Raise Error when form has punctuation django -



python - Raise Error when form has punctuation django -

i need way raise error when submit punctuation in title. im beginner im not quite sure how so. form:

class neededform(forms.modelform): title = forms.charfield(max_length=120) likes = forms.integerfield(widget=forms.hiddeninput(), initial=0) body = forms.charfield(min_length=50,widget = forms.textarea) captcha = captchafield() def clean_title(self): info = self.cleaned_data['title'] homecoming info class meta: model = needed fields = ('title', 'body', 'likes')

the view:

def detail(request, needed_title_url): context = requestcontext(request) needed_name = needed_title_url.replace('_', ' ') context_dict = {'needed_name': needed_name} try: needed = needed.objects.get(title=needed_name) context_dict['needed'] = needed print "true!" except: pass homecoming render_to_response('needed.html', context_dict, context)

if user inputs like: i chicken! title need show user error.

i'm not totally sure question, looks reply looking may included in https://docs.djangoproject.com/en/1.6/ref/models/fields/#slugfield

the slugfield charfield can contain letters, numbers, underscores , hyphens (so understands max_length,...)

class neededform(forms.modelform): title = forms.slugfield(max_length=120) likes = forms.integerfield(widget=forms.hiddeninput(), initial=0) body = forms.charfield(min_length=50,widget = forms.textarea) captcha = captchafield()

i looking more complicated rule checking, have validator page https://docs.djangoproject.com/en/1.6/ref/validators/ , how utilize in form https://docs.djangoproject.com/en/1.6/ref/forms/validation/#using-validators illustration provided explaining slugfield implementation.

hope helps !

python django django-forms

No comments:

Post a Comment