简体   繁体   中英

Django execute a custom function from Admin

I am new to Django and have gone through the first Django App tutorial.I understand the separate bits but am not able to form a complete solution.

My requirement is to add a custom button to a Model's Change List Page in Admin, redirect it to a confirmation page, execute a function on confirmation and then redirect to the change list page with execution status.

I have added the custom button but am lost after that. Currently this is what I am doing:

Changed the admin class list template

class ReconciliationAdmin(admin.ModelAdmin):
    change_list_template = 'SalesReconciliation/change_list.html'

This is my change list template:

{% extends "admin/change_list.html" %}
{% load i18n admin_static %}

{% block object-tools-items %}
{{ block.super }}
<li>
    <a href={% url 'admin:reconcileConfirm'%} class="btn btn-high btn-success">Run Reconciliation</a>
</li>
{% endblock %}

Changed the urls.py in the main app as follows:

urlpatterns = [
    url(r"^reconcileConfirm/", include("SalesReconciliation.urls")),
]

Changed the urls.py in the child app as follows:

urlpatterns = [
    url(r'^reconcileConfirm/', views.ReconcileConfirm, name='ReconcileConfirm')
]

In the views.py of the child app added the follows:

def ReconcileConfirm(request):
    return HttpResponse("..something")

I am hoping that this would work like:

Click on Run Reconciliation link -> Main urls.py routes to child urls.py -> child urls.py routes to views.py -> function in views py performs some operations

But I am getting the following errors:

NoReverseMatch at /SalesReconciliation/reconciliation/ Reverse for 'reconcileConfirm' not found. 'reconcileConfirm' is not a valid view function or pattern name.

Request Method: GET

Request URL: http://127.0.0.1:8000/SalesReconciliation/reconciliation/

Django Version: 2.0.4

Exception Type: NoReverseMatch

Exception Value: Reverse for 'reconcileConfirm' not found.

'reconcileConfirm' is not a valid view function or pattern name.

Also I would like to know whether it is the correct approach.

In your template:

{% url 'admin:reconcileConfirm'%}

in urls.py:

urlpatterns = [
    url(r'^reconcileConfirm/', views.ReconcileConfirm, name='ReconcileConfirm')
]

Note that ReconcileConfirm is different from reconcileConfirm . This is probably the first error to fix in order to achieve what you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM