Django modify model instance from HTML

49 views
Skip to first unread message

Tobias Dacoir

unread,
Jan 9, 2015, 4:24:57 AM1/9/15
to django...@googlegroups.com

I want to know if it is possible to directly modify and save a model instance in the HTML Template and not via a view and extra URL.

My user has a Boolean Property, I want to display it as a toggle button on the website and the user should be able to toggle it on or off without leaving the website or reloading it.

class User(AbstractBaseUser, PermissionsMixin): ... autoplay_enabled = models.BooleanField(default=True) ...

Is this possible without an extra view or form?
Basically I just need to set

request.user.autoplay_enabled = False

(or True)

and then save() it

If I can't modify the object directly in the HTML template is it at least possible to just execute a function I have defined somewhere in my Python code, without having the need to create a new view?

James Schneider

unread,
Jan 9, 2015, 6:19:09 AM1/9/15
to django...@googlegroups.com

You'll need to look into JavaScript or one of the JS libraries such as JQuery to implement the functionality you want. The JS will fire off a request back to Django to make the update at the moment the box is checked. You can use the same view with some complicated logic, but I would recommend the update be handled as an API call and use a separate URL and view for readability and explicit behavior, especially if that action will be used on multiple pages, but perhaps acting on different objects.

Modifying the object in the template only affects things before they are displayed to the user.

One the page is rendered/displayed to the user, Django has no control or influence until another request is made, hence the JS in the local browser asking your Django instance for the update behind the scenes, at which point your view can fire off any Python function you wish.

AJAX would be a good search term.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/abed6766-0bb5-40d9-a2b3-065e17633937%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tobias Dacoir

unread,
Jan 9, 2015, 7:32:08 AM1/9/15
to django...@googlegroups.com
Thanks for the answer. I will implement an AJAX call to another url / view than. This I should be able to figure out using a Button and Google.
The URL can be anything like /settings/autoplay/. The view just has to update the property (user object should be in the request already). However, what should the view return?

James Schneider

unread,
Jan 9, 2015, 11:26:00 AM1/9/15
to django...@googlegroups.com

Yep, the URL can be anything. Check out https://docs.djangoproject.com/en/1.7/topics/class-based-views/generic-editing/#ajax-example for an example using CBV's. For the API call, you won't end up rendering any html, just returning an HTTPResponse object and perhaps some data, likely in JSON format.

You can also look at Django-braces, which provides mixins for handling AJAX, or Django-rest-framework or Django-tastypie for API handling.

-James

On Jan 9, 2015 4:32 AM, "Tobias Dacoir" <fal...@gmail.com> wrote:
Thanks for the answer. I will implement an AJAX call to another url / view than. This I should be able to figure out using a Button and Google.
The URL can be anything like /settings/autoplay/. The view just has to update the property (user object should be in the request already). However, what should the view return?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages