-
-
Save rsarai/d475c766871f40e52b8b4d1b12dedea2 to your computer and use it in GitHub Desktop.
from django import forms | |
from django.contrib import admin | |
from django.template.response import TemplateResponse | |
class RegularCreationAdminForm(forms.ModelForm): | |
class Meta: | |
model = ReactivationCoupon | |
fields = [ | |
'field1', 'field2', 'field3', 'field4', | |
'field5', 'field6', | |
] | |
class RegularCreateViewAdmin(admin.ModelAdmin): | |
list_display = ('field1', 'field2', 'field3', 'field4', 'field5', 'field6') | |
search_fields = ('field1',) | |
ordering = ('field1', 'field2',) | |
form = RegularCreationAdminForm | |
def _changeform_view(self, request, object_id, form_url, extra_context): | |
if request.method == 'POST' and "_confirmation" in request.POST and not object_id: | |
ModelForm = self.get_form(request) | |
form = ModelForm(request.POST, request.FILES) | |
form_validated = form.is_valid() | |
if form_validated: | |
new_object = form.save(commit=False) | |
context = { | |
**self.admin_site.each_context(request), | |
'obj': new_object, | |
'opts': self.model._meta, | |
'form': form, | |
} | |
return TemplateResponse( | |
request, 'admin/app_name/model_name/form_confirmation.html', context) | |
return super()._changeform_view(request, object_id, form_url, extra_context) |
{% extends "admin/change_form.html" %} | |
{% block submit_buttons_bottom %} | |
<input type="submit" value="Confirm" name="_confirmation"> | |
{% endblock %} |
{% extends "admin/base_site.html" %} | |
{% load i18n l10n admin_urls %} | |
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} delete-confirmation | |
delete-selected-confirmation{% endblock %} | |
{% block breadcrumbs %} | |
<div class="breadcrumbs"> | |
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a> | |
› <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a> | |
› <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a> | |
› {{ title }} | |
</div> | |
{% endblock %} | |
{% block content %} | |
<ul style="padding: 0"> | |
This is a confirmation view! | |
</ul> | |
<hr> | |
<br> | |
<form action="" method="post">{% csrf_token %} | |
<div class="submit-row"> | |
<input type="submit" name="_save" value="{% trans "Yes, do it!" %}"/> | |
<a href="#" onclick="window.history.back(); return false;" | |
class="button cancel-link">{% trans "No, take me back" %}</a> | |
</div> | |
<fieldset class="module aligned" style="visibility: hidden;"> | |
<div class="form-row hidden"> | |
{{ form }} | |
</div> | |
</fieldset> | |
</form> | |
{% endblock %} |
@JuneZho Yes, Django deals with inline formsets a bit different. I have modified the
_changeform_view
just for the simplest case. My advice to you is to use the full method available here, and only perform the redirect after the inline_instances are created. Let me know if this is useful to you
Thank you for your help on customizing _changeform_view
to have confirmation page. I also had problem with inline formsets and figured out a way to deal with it by adding this to context:
formsets, _ = self._create_formsets(request, attribute, change=True)
Then, next thing to do is rendering all formsets in template:
{% for formset in formsets %}
{{ formset }}
{% endfor %}
If we want confirmation for a single admin model..is there a way to make it work?
@JuneZho Yes, Django deals with inline formsets a bit different. I have modified the
_changeform_view
just for the simplest case. My advice to you is to use the full method available here, and only perform the redirect after the inline_instances are created. Let me know if this is useful to you