T
T
The-TS2021-05-23 19:18:31
Django
The-TS, 2021-05-23 19:18:31

How to add custom button to model change page, Django?

Hey!

How to make a button like this:
custom_button.png

PS: I looked at this instruction, but the button does not appear for me

The code:

~/project/app/admin/change_form.html:
{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
    {{ block.super }}
    <div class="submit-row">
        <input type="submit" value="Make Unique" name="_make-unique">
    </div>
{% endblock %}

~/project/app/admin.py:
from django.contrib import admin
from django.http.response import HttpResponseRedirect
from .models import myself_ads, product, Order

@admin.register(myself_ads)
class myself_ads_admin(admin.ModelAdmin):
    list_display = ('title',)

@admin.register(product)
class product_admin(admin.ModelAdmin):
    change_form_template = "admin/change_form.html"
    list_display = ('name', 'material')

    def response_change(self, request, obj):
        if "_make-unique" in request.POST:
            self.message_user(request, "This villain is now unique")
            return HttpResponseRedirect(".")
        return super().response_change(request, obj)

@admin.register(Order)
class Order_admin(admin.ModelAdmin):
    list_display = ('user', 'target', 'status', 'canceled')
    list_filter = ('status', 'Paided', 'canceled')
    search_fields = ('id',)
    readonly_fields = ["id"]



Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maksam07, 2021-05-23
@Pyhon3x

Why do you refer in the class to the standard template - "admin/change_form.html" ? (change_form_template = "admin/change_form.html")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question