Sunday, August 28, 2011

Model.save() does nothing

I'm playing around with Django at the moment and have a strange
problem.

There is a view function which basicly does nothing else than toggle a
boolean value in a model field - or at least should do so.

The field definition:

class Character(models.Model):
...
ready = models.BooleanField(default=False)
...

The view function:

@decorators.login_required
def character_toggle_ready(request, id):
character = get_object_or_404(Character, pk=id)
character.ready = not character.ready
character.save()
return redirect(request.META.get('HTTP_REFERER', '/accounts/
overview'))

The url conf:
urlpatterns = patterns('',
...
url(r'^character/toggle_ready/(\d+)/$',
views.character_toggle_ready),
...)

In practice I want to have the ready-value toggled when clicking on a
hyperlink on my site. E.g. when clicking on '/character/toggle_ready/
1', the value should be toggled and then the view function should
redirect to the page before.

That works as intended, when I access the view from a template which
belongs to the app in which this view is definded. But strangely it
does not work if the link is in a different app. So, when the toggle-
view lies in the app 'accounts', it's no problem when accessing it
from a template that's rendered by another view of this app. But when
the template comes from the app 'core', it wouldn't save. The
redirection still works as a charm and also the object is retrieved
and the value is toggled, I've checked that with print-functions. But
after the save seems to be before the save.

Has anybody an idea what the reason for that behavior could be? Is the
save-function dependend on the passed request? Or am I overlooking
something?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment