How can I add a button into django admin change list view page

后端 未结 2 1974
广开言路
广开言路 2020-12-04 18:24

I would like to add a button next to \"add\" button in list view in model for my model and then create a view function where I will do my stuff and then redirect user back t

2条回答
  •  有刺的猬
    2020-12-04 19:07

    When several applications provide different versions of the same resource (template, static file, management command, translation), the application listed first in INSTALLED_APPS has precedence. - Django documentation on INSTALLED_APPS

    Make sure your app is listed before 'django.contrib.admin' in INSTALLED_APPS.

    Create a change_list.html template in one of the following directories:

    # Template applies to all change lists.
    myproject/myapp/templates/admin/change_list.html      
    
    # Template applies to change lists in myapp.
    myproject/myapp/templates/admin/myapp/change_list.html  
    
    # Template applies to change list in myapp and only to the Foo model.
    myproject/myapp/templates/admin/myapp/foo/change_list.html  
    

    The template should be picked up automatically, but in case it is not on one of paths listed above, you can also point to it via an admin model attribute:

    class MyModelAdmin(admin.ModelAdmin):
    
        #... 
        change_list_template = "path/to/change_list.html"
    

    You can lookup the contents of the original change_list.html it lives in path/to/your/site-packages/django/contrib/admin/templates/admin/change_list.html. The other answer also shows you how to format the template. Nikolai Saiko shows you how to override the relevant parts using 'extends' and 'super'. Summary:

    {% extends "admin/change_list.html" %} {% load i18n %} 
    {% block object-tools-items %}
        {{ block.super }}
        
  • My custom admin page
  • {% endblock %}

    Let's fill href="..." with an url. The admin url names are in the namespace 'admin' and can be looked up like this:

    {% url 'admin:custom_view' %}
    

    When you are adding a button to change_form.html you maybe want to pass in the current object id:

    {% url 'admin:custom_view' original.pk %}
    

    Now create a custom view. This can be a regular view (just like other pages on your website) or a custom admin view in admin.py. The get_urls method on a ModelAdmin returns the URLs to be used for that ModelAdmin in the same way as a URLconf. Therefore you can extend them as documented in URL dispatcher:

    class MyModelAdmin(admin.ModelAdmin):
        def get_urls(self):
            urls = super(MyModelAdmin, self).get_urls()
            my_urls = patterns('',
                url(r'^my_view/$', self.my_view, name="custom_view")
            )
            return my_urls + urls
    
        def my_view(self, request):
            # custom view which should return an HttpResponse
            pass
    
        # In case your template resides in a non-standard location
        change_list_template = "path/to/change_list.html"
    

    Read the docs on how to set permissions on a view in ModelAdmin: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls

    You can protect your view and only give access to users with staff status:

    from django.contrib.admin.views.decorators import staff_member_required
    
    @staff_member_required
    def my_view(request):
        ...
    

    You might also want to check request.user.is_active and handle inactive users.

    Update: Take advantage of the framework and customise as little as possible. Many times actions can be a good alternative: https://docs.djangoproject.com/en/1.5/ref/contrib/admin/actions/

    Update 2: I removed a JS example to inject a button client side. If you need it, see the revisions.

提交回复
热议问题