Django administration provides three buttons for submitting the currently edited object. Each of them has a unique name and depending on the name that is sent to the server, the specific action is performed. I see this as an ugly solution and prefer to have a choice field in the form which would render as submit buttons with different values. Then the values would be checked instead of the names. Therefore, I created the `MultipleSubmitButton` widget. When `<input type="submit" value="Go" />` is used, the value sent to the server always matches the text on the button, but if `<button type="submit" value="go">Go</button>`, the value and the human representation might differ.
To use the `MultipleSubmitButton` widget, pass it to the widget parameter of a `ChoiceField` like this:
SUBMIT_CHOICES = (
('save', _("Save")),
('save-add', _("Save and Add Another")),
)
class TestForm(forms.Form):
do = forms.ChoiceField(
widget=MultipleSubmitButton,
choices=SUBMIT_CHOICES,
)
When you print `{{ form.do }}` in the template, the following HTML will be rendered:
<ul>
<li><button type="submit" name="do" value="save">Save</button></li>
<li><button type="submit" name="do" value="save-add">Save and Add Another</button></li>
</ul>
When you submit this form and check the validity of it, `form.cleaned_data['do']` will return "save" or "save-add" depending on the submit button clicked.
In the past, whenever I had a script that I wanted to properly configure the settings for, I would use something like the following idiom at the top of the script:
import sys, os; dirname = os.path.dirname
# sys.path.insert(0, dirname(dirname(__file__)))
sys.path.insert(0, dirname(__file__))
os.environ['DJANGO_SETTINGS_MODULE'] = 'myapp.settings'
Notice that this is a relative setting to `__file__` variable in the script. The djangopath function is an attempt to do away with the above such that I can now write the following:
from lib import djangopath; djangopath(up=2, settings='myapp.settings')
This seems to work for me, but it assumes that you are packaging your script inside your projects/apps. If they are elsewhere then you may need to resort to another method (e.g. absolute paths, etc.)
AK