Django Bootstrap Extra Form Buttons

This is how I have been implementing extra buttons on my forms via Django Bootstrap.

# In context of form model
class XUpdateForm(forms.ModelForm):
...
def clean(self):
	cleaned_data = super(XUpdateForm, self).clean()

	# Add action to clean data
	cleaned_data['action'] = self.data['action']

	return cleaned_data

Now in the view responsible for this form

form = XUpdateForm(request.POST)

obj = form.save(commit=False)
obj = q_id
obj = user_id

# Process valid forms only
if form.is_valid():
	action = form.cleaned_data['action']

In the template that renders the form and buttons, we add name and value arguments to our boostrap_button calls.

{% bootstrap_button "Update" name="action" value="update" button_type="submit" %}
{% bootstrap_button "Remove" name="action" value="remove" button_type="submit" %}

Happy coding!

Dec 9th, 2017 • Posted in Django, Python
No comments yet.

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>