D
D
Dmitry Varenkov2016-06-26 00:51:10
Django
Dmitry Varenkov, 2016-06-26 00:51:10

How to program a button in Django admin?

Good day,
I can't figure out how to trigger an action on a button in the Django admin panel. I overridden
change_list.html for the model:

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

{% block object-tools %}
  {% if has_add_permission %}
  <ul class="object-tools">
    {% block object-tools-items %}
      <li>
        <button type="submit" name="getShard" value="getShard" class="button">Get Shard</button>
      </li>
      <li>
        <button type="submit" name="refreshShard" value="refreshShard" class="button">Refresh Shard</button>
      </li>
    {% endblock %}
  </ul>
  {% endif %}
{% endblock %}

Wrote conditional functions in admin.py:
def getShard():
  pass

def refreshShard():
  pass

Can you please tell me how to bind functions to buttons? Or where can I find material on the topic?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Z
zelsky, 2016-06-26
@zelsky

so after all pass inactivity. instead of a pass, write a redirect at least

S
sim3x, 2016-06-26
@sim3x

https://www.google.com/search?q=django+admin+custo...

O
Oscar Django, 2016-06-26
@winordie

class MyModelAdmin(admin.ModelAdmin):
    def get_urls(self):
        urls = super().get_urls()
        shard_urls = [
            url(r'^get_shard/$', getShard),
            url(r'^refresh_shard/$', refreshShard),
        ]
        return shard_urls + urls

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question