I want to create a GenericFormMixin for Django that I can mix into CreateView and UpdateView that will let me use a generic form template by dynamically setting things like the page title, form title, submit button text, etc.
I have the following in mixins.py:
class GenericFormMixin(object):
page_title = ''
form_title = ''
submit_button_text = ''
Instead of having to create a modelname_create.html and modelname_update.html for every CreateView or UpdateView I have, I want to be able to use generic_form.html which will use the mixin's variables in its context to dynamically create an appropriately populated template:
{% extends "base.html" %}
{% block title %}{{ page_title }}{% endblock title %}
{% block content %}
<div class="panel panel-primary">
<div class="panel-heading">{{ form_title }}</div>
<div class="panel-body">
<form method="post" action=".">
{{ form }}
{% csrf_token %}
<button type="submit" class="btn btn-primary">{{ submit_button_text }}</button>
</form>
</div>
</div>
{% endblock content %}
My question is: now that I've created the mixin, how do I get the variables into the template context of CreateView and UpdateView?
Define get_context_data
in your mixin. It should call the superclass method, add its elements to the dictionary returned from there, and then return the dict.