I have a a contact & info page that uses flatpages. However I want to add a form that allows visitors to sign their emails up to a list. However, if I send the URL to render the form view, it won't have the flatpages context. What is the best way to either include the flatpages context on my email form view, or add my form context to my flatpage.
Thanks to Tommasa Barbugli for the solution. I now have:
from django.contrib.flatpages.models import FlatPage
from django.shortcuts import render, get_object_or_404
from canada.bulkmail.models import *
def email(request):
contact_flatpage = get_object_or_404(FlatPage, url='/contact/')
ContactEmailFormSet = modelformset_factory(Contact, formset=ContactEmailForm)
if request.method == 'POST':
formset = ContactEmailFormSet(request.POST)
if formset.is_valid():
contact = formset.save()
else:
formset = ContactEmailFormSet()
return render(request, 'flatpages/default.html', {
"formset": formset,
"flatepage": contact_flatpage,
})
Return the right context from the form page view.
This is the context sent to flatpages' template (where f is the flatpage instance)
f.title = mark_safe(f.title)
f.content = mark_safe(f.content)
c = RequestContext(request, {
'flatpage': f,
})
in your view you can easily return a (unsaved) flatpage instance with all attrs you need.
f = FlatPage()
f.title = ""
f.content = ""
....
c = RequestContext(request, {
'flatpage': f,
})