kopia lustrzana https://github.com/rtts/django-simplecms
Better form handling
rodzic
e9fddf8a2e
commit
4a8c804452
|
@ -113,8 +113,7 @@ class ContactForm(forms.Form):
|
||||||
spam_protection = forms.CharField(label=_('Your message'), widget=forms.Textarea())
|
spam_protection = forms.CharField(label=_('Your message'), widget=forms.Textarea())
|
||||||
message = forms.CharField(label=_('Your message'), widget=forms.Textarea(), initial='Hi there!')
|
message = forms.CharField(label=_('Your message'), widget=forms.Textarea(), initial='Hi there!')
|
||||||
|
|
||||||
def save(self, request):
|
def save(self):
|
||||||
hostname = request.get_host()
|
|
||||||
body = self.cleaned_data.get('spam_protection')
|
body = self.cleaned_data.get('spam_protection')
|
||||||
if len(body.split()) < 7:
|
if len(body.split()) < 7:
|
||||||
return
|
return
|
||||||
|
@ -125,7 +124,7 @@ class ContactForm(forms.Form):
|
||||||
email = EmailMessage(
|
email = EmailMessage(
|
||||||
to = [settings.DEFAULT_TO_EMAIL],
|
to = [settings.DEFAULT_TO_EMAIL],
|
||||||
body = body,
|
body = body,
|
||||||
subject = _('Contact form at %(hostname)s.') % {'hostname': hostname},
|
subject = _('Contact form'),
|
||||||
headers = {'Reply-To': self.cleaned_data.get('sender')},
|
headers = {'Reply-To': self.cleaned_data.get('sender')},
|
||||||
)
|
)
|
||||||
email.send()
|
email.send()
|
||||||
|
|
|
@ -11,7 +11,6 @@
|
||||||
{% if form.errors %}
|
{% if form.errors %}
|
||||||
<div class="global_error">
|
<div class="global_error">
|
||||||
{% trans 'Please correct the error(s) below and save again' %}
|
{% trans 'Please correct the error(s) below and save again' %}
|
||||||
{{form.errors}}
|
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ class IncludeSectionNode(template.Node):
|
||||||
'perms': perms,
|
'perms': perms,
|
||||||
}
|
}
|
||||||
if hasattr(section, 'invalid_form'):
|
if hasattr(section, 'invalid_form'):
|
||||||
context['form'] = section.invalid_form
|
initial_context['form'] = section.invalid_form
|
||||||
|
|
||||||
section_context = view.get_context_data(**initial_context)
|
section_context = view.get_context_data(**initial_context)
|
||||||
t = context.template.engine.get_template(view.template_name)
|
t = context.template.engine.get_template(view.template_name)
|
||||||
|
|
44
cms/views.py
44
cms/views.py
|
@ -15,23 +15,37 @@ class SectionView:
|
||||||
template_name = 'cms/sections/section.html'
|
template_name = 'cms/sections/section.html'
|
||||||
|
|
||||||
def __init__(self, request):
|
def __init__(self, request):
|
||||||
'''Initialize request attribute'''
|
|
||||||
self.request = request
|
self.request = request
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
'''Override this to customize a section's context'''
|
|
||||||
return kwargs
|
return kwargs
|
||||||
|
|
||||||
class SectionFormView(edit.FormMixin, SectionView):
|
class SectionFormView(SectionView):
|
||||||
'''Generic section with associated form'''
|
'''Generic section with associated form'''
|
||||||
|
form_class = None
|
||||||
|
success_url = None
|
||||||
|
|
||||||
def post(self, request):
|
def get_context_data(self, **kwargs):
|
||||||
'''Process form'''
|
if 'form' not in kwargs:
|
||||||
form = self.get_form()
|
kwargs['form'] = self.get_form()
|
||||||
if form.is_valid():
|
return kwargs
|
||||||
form.save(request)
|
|
||||||
return HttpResponseRedirect(self.get_success_url())
|
def form_valid(self, form):
|
||||||
return form
|
form.save()
|
||||||
|
return HttpResponseRedirect(self.success_url)
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
return {}
|
||||||
|
|
||||||
|
def get_form(self, method='get'):
|
||||||
|
form_class = self.form_class
|
||||||
|
kwargs = self.get_form_kwargs()
|
||||||
|
if method == 'post':
|
||||||
|
kwargs.update({
|
||||||
|
'data': self.request.POST,
|
||||||
|
'files': self.request.FILES,
|
||||||
|
})
|
||||||
|
return form_class(**kwargs)
|
||||||
|
|
||||||
class PageView(detail.DetailView):
|
class PageView(detail.DetailView):
|
||||||
'''View of a page with heterogeneous sections'''
|
'''View of a page with heterogeneous sections'''
|
||||||
|
@ -51,8 +65,6 @@ class PageView(detail.DetailView):
|
||||||
page = registry.page_class(title='Homepage', slug='')
|
page = registry.page_class(title='Homepage', slug='')
|
||||||
page.save()
|
page.save()
|
||||||
self.object = page
|
self.object = page
|
||||||
elif self.request.user.has_perm('cms_page_create'):
|
|
||||||
return redirect('cms:updatepage', self.kwargs['slug'])
|
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
context = self.get_context_data(**kwargs)
|
context = self.get_context_data(**kwargs)
|
||||||
|
@ -76,10 +88,10 @@ class PageView(detail.DetailView):
|
||||||
for section in sections:
|
for section in sections:
|
||||||
if section.pk == pk:
|
if section.pk == pk:
|
||||||
view = registry.get_view(section, request)
|
view = registry.get_view(section, request)
|
||||||
result = view.post(request)
|
form = view.get_form(method='post')
|
||||||
if isinstance(result, HttpResponse):
|
if form.is_valid():
|
||||||
return result
|
return view.form_valid(form)
|
||||||
section.invalid_form = result
|
section.invalid_form = form
|
||||||
|
|
||||||
context.update({
|
context.update({
|
||||||
'page': page,
|
'page': page,
|
||||||
|
|
|
@ -205,16 +205,6 @@ nav {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
div.edit {
|
|
||||||
position: fixed;
|
|
||||||
right: 1em;
|
|
||||||
bottom: 1em;
|
|
||||||
z-index: 1000;
|
|
||||||
img {
|
|
||||||
width: 75px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
section {
|
section {
|
||||||
clear: both;
|
clear: both;
|
||||||
position: relative;
|
position: relative;
|
||||||
|
|
Ładowanie…
Reference in New Issue