开发者

Adding custom button or a tag to django admin

I want to add some html element开发者_C百科 (button, "a" tag, etc ) to a django admin page. How can i do it? Please help.


Not sure where you want to add your stuff but this is a solution I found somewhere else on SO to change the HTML of a FileField (in my case i wanted to display the current image in an ImageField).

In other words you can make a widget that modifies the html of the field you want to customize:

# Widget that modifies the output of a FileField
class OutputWidget(AdminFileWidget):
    # Overloaded django magic
    def render(self, name, value, attrs=None):
        output = []
        # This is the place where we edit the output    
        if value and getattr(value, "url", None):
            image_url = value.url
            output.append(u' <a href="%s" target="_blank"><img src="%s" alt="%s" /></a>' % (image_url, image_url, image_url))
        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))

# ModelAdmin class that is applied to the model
class MyModelSettings(admin.ModelAdmin):
    # Overloaded django magic
    def formfield_for_dbfield(self, db_field, **kwargs):
        # Look for the field we want to edit and register the widget with it
        if db_field.name == 'nameOfFieldIWantToEdit':
            request = kwargs.pop("request", None)
            kwargs['widget'] = OutputWidget
            return db_field.formfield(**kwargs)
        return super(MyModelSettings,self).formfield_for_dbfield(db_field, **kwargs)

# Register my overloaded settings with the model
admin.site.register(MyModel, MyModelSettings)

The code goes into admin.py where you register your models.


From the docs:

  • The Django admin site
  • Customizing the Django admin interface


I used Omokoli's solution from above but to make the field use my custom widget I did:

class MyModelAdminForm(forms.ModelForm):
    class Meta:
        model = get_model('myapp', 'mymodel')
        widgets = {
            'original_link': OutputWidget,
        }


  1. You can create a file under templates/admin called base_site.html (create the "admin" folder in your app).
  2. add this code:
{% extends "admin/base_site.html" %}
{% load i18n %}


{% block userlinks %}
{% if site_url %}
    <a href="{{ site_url }}">{% trans 'View site' %}</a> /
{% endif %}
{% if user.is_active and user.is_staff %}
    {% url 'django-admindocs-docroot' as docsroot %}
    {% if docsroot %}
        <a href="{{ docsroot }}">{% trans 'Documentation' %}</a> /
    {% endif %}
{% endif %}
{% if user.has_usable_password %}
<a href="{% url 'admin:password_change' %}">{% trans 'Change password' %}</a> /
{% endif %}
<a href="{% url 'admin:logout' %}">{% trans 'Log out' %}</a>

<!-- YOUR CUSTOM CODE HERE -->
<div class="your_custom_class">
    <a href="{% url 'your_app:yourlink1' %}">{% trans 'your link 1' %}</a> /
    <a href="{% url 'your_app:yourlink2' %}">{% trans 'your link 2' %}</a>
</div>

{% endblock %}

You can overwrite a lot of the admin html files. Make sure you extends the html you are overwriting, not to loose basic functionality.

See customize-django-admin-python for full admin templates tree you can overwrite.

You can also look at the base_site.html in Django project in Github. You'll find all of the "base_site" blocks you can plant your custom code in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜