Django Formset management-form validation error
Posted
by gramware
on Stack Overflow
See other posts from Stack Overflow
or by gramware
Published on 2010-03-29T07:10:58Z
Indexed on
2010/03/29
7:13 UTC
Read the original article
Hit count: 671
I have a form and a formset on my template. The problem is that the formset is throwing validation error claiming that the management form is "missing or has been tampered with".
Here is my view
@login_required
def home(request):
user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
blogz = list(blog.objects.filter(deleted='0'))
delblog = modelformset_factory(blog, exclude=('poster','date' ,'title','content'))
if request.user.is_staff== True:
staff = 1
else:
staff = 0
staffis = 1
if request.method == 'POST':
delblogformset = delblog(request.POST)
if delblogformset.is_valid():
delblogformset.save()
return HttpResponseRedirect('/home')
else:
delblogformset = delblog(queryset=blog.objects.filter( deleted='0'))
blogform = BlogForm(request.POST)
if blogform.is_valid():
blogform.save()
return HttpResponseRedirect('/home')
else:
blogform = BlogForm(initial = {'poster':user.id})
blogs= zip(blogz,delblogformset.forms)
paginator = Paginator(blogs, 10) # Show 25 contacts per page
# Make sure page request is an int. If not, deliver first page.
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
# If page request (9999) is out of range, deliver last page of results.
try:
blogs = paginator.page(page)
except (EmptyPage, InvalidPage):
blogs = paginator.page(paginator.num_pages)
return render_to_response('home.html', {'user':user, 'blogform':blogform, 'staff': staff, 'staffis': staffis, 'blog':blogs, 'delblog':delblogformset}, context_instance = RequestContext( request ))
my template
{%block content%}
<h2>Home</h2>
{% ifequal staff staffis %}
{% if form.errors %}
<ul>
{% for field in form %}
<H3 class="title">
<p class="error"> {% if field.errors %}<li>{{ field.errors|striptags }}</li>{% endif %}</p>
</H3>
{% endfor %}
</ul>
{% endif %}
<h3>Post a Blog to the Front Page</h3>
<form method="post" id="form2" action="" class="infotabs accfrm">
{{ blogform.as_p }}
<input type="submit" value="Submit" />
</form>
<br>
<br>
{% endifequal %}
<div class="pagination">
<span class="step-links">
{% if blog.has_previous %}
<a href="?page={{ blog.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ blog.number }} of {{ blog.paginator.num_pages }}.
</span>
{% if blog.has_next %}
<a href="?page={{ blog.next_page_number }}">next</a>
{% endif %}
</span>
<form method="post" action="" class="usertabs accfrm">
{{delblog.management_form}}
{% for b, form in blog.object_list %}
<div class="blog">
<h3>{{b.title}}</h3>
<p>{{b.content}}</p>
<p>posted by <strong>{{b.poster}}</strong> on {{b.date}}</p>
{% ifequal staff staffis %}<p>{{form.as_p}}<input type="submit" value="Delete" /></p>{% endifequal %}
</div>
{% endfor %}
</form>
{%endblock%}
© Stack Overflow or respective owner