@render_to('')
def myblog_add(request, form_class=MyBlogForm, template="pages/
myblog_add.html"):
if request.method == "POST":
avatar = None
readonly=False
myblog_form = form_class(request.POST, request.user)
if myblog_form.is_valid():
myblog_form.save()
return redirect(reverse('myblog'))
else:
try:
avatar = Avatar.objects.get(primary=True,
user=request.user)
myblog_form = form_class(user=request.user)
readonly = False
except Avatar.DoesNotExist:
myblog_form = form_class(request.user, readonly=True)
readonly = True
avatar = None
return {
'avatar':avatar,
'readonly':readonly,
'form':myblog_form,
}, template
форма
class MyBlogForm(forms.Form):
blog_title = forms.CharField(label=_(u"Заголовок"), required=True,
min_length=5, max_length=200)
blog_body = forms.CharField(label=_(u"Текст"),widget=TinyMCE(),
min_length=100, max_length=5000)
blog_tags = forms.CharField(label=_(u"Теги"), required=False)
def __init__(self, user=None, readonly=False, *args, **kwargs):
self.user = user
super(MyBlogForm, self).__init__(*args, **kwargs)
if readonly:
self.fields['blog_title'].widget.attrs['disabled'] =
'disabled'
self.fields['blog_body'].widget = forms.Textarea()
self.fields['blog_body'].widget.attrs['disabled'] =
'disabled'
self.fields['blog_tags'].widget.attrs['disabled'] =
'disabled'
def clean(self):
assert False, 'test'
return self.cleaned_data
def save(self):
avatar = Avatar.objects.get(primary=True, user=self.user)
blog = Post(
title = self.cleaned_data['blog_title'],
body = self.cleaned_data['blog_body'],
author = self.user,
slug = slugify(unidecode(u'%s' %
self.cleaned_data['blog_title'])),
title_image = avatar,
status = 1,
allow_comments = True,
#categories
tags = self.cleaned_data['blog_tags'],
)
blog.save()
----
myblog_form.is_valid() - дает False
в форме clean() даже не выполняеться :(
в чем причина?
Комментариев нет:
Отправить комментарий