Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Django Admin Intermediate page

I need a way to have an intermediate page shown when I´ve saved a model in django admin.

What I want to accomplish is after "saving" a model, show a page with all the attributes of the model lined up and then have a button that says Print. I used to solve this with Jquery dialog div when clicking save. That meant that I showed the settings print view before actually saving the model but I need the model to validate first now.

Its like the way that the "delete model" action is implemented. I just can´t seem to find out where to start looking though.

Edit: I´ve started looking in the django.contrib.admin.options.py for the response_change and response_add methods. Not sure how to override them though. And its only needed for one specific model so its not generic. Also I´ve discovered the list of templates in the Class ModelAdmin. Still not sure about how to proceed without hacking the admin to bits.

Edit 2: Added my working solution down below.

like image 310
Gesias Avatar asked Mar 02 '13 02:03

Gesias


People also ask

How do I get to the admin page in Django?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

Can Django admin be used in production?

Django's Admin is amazing. A built-in and fully functional interface that quickly gets in and allows data entry is priceless. Developers can focus on building additional functionality instead of creating dummy interfaces to interact with the database.

Is Django's admin interface customizable if yes then how?

To implement it in your project, make a new app in your Django project named products. Install this application, type product in the INSTALLED_APPS list in settings.py file. We will now make models in the products app. The model will be used throughout the tutorial to customize the Django Admin.


1 Answers

You could create a form that had an extra step of validation for the 'are you sure' step.

Given this model in our models.py:

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=100)

Add a form in forms.py:

from django import forms
from .models import Person

class PersonForm(forms.ModelForm):
    i_am_sure = forms.BooleanField(required=False, widget=forms.HiddenInput())

    def __init__(self, *args, **kwargs):
        super(PersonForm, self).__init__(*args, **kwargs)

        if self.errors.get('i_am_sure'):
            # show the 'are you sure' checkbox when we want confirmation
            self.fields['i_am_sure'].widget = forms.CheckboxInput()

    def clean(self):
        cleaned_data = super(PersonForm, self).clean()

        if not self.errors:
            # only validate i_am_sure once all other validation has passed
            i_am_sure = cleaned_data.get('i_am_sure')
            if self.instance.id and not i_am_sure:
                self._errors['i_am_sure'] = self.error_class(["Are you sure you want to change this person?"])
                del cleaned_data['i_am_sure']

        return cleaned_data

    class Meta:
        model = Person

If you want to use this with Django admin. specify this form in your admin.py:

from django.contrib import admin
from .forms import PersonForm
from .models import Person

class PersonAdmin(admin.ModelAdmin):
    form = PersonForm

admin.site.register(Person, PersonAdmin)

Note however that there's a bug with hidden inputs on Django admin forms. There's a solution to that on this Stack Overflow question.

like image 107
Wilfred Hughes Avatar answered Sep 30 '22 11:09

Wilfred Hughes