Sergio
Sergio

Reputation: 4567

Django admin site rename URLs to match my language

I would like to customize the URLs for the admin site so they appear in my native language (Spanish). What I want is to use /nuevo instead of /add and so on. Is this possible?

Upvotes: 1

Views: 339

Answers (1)

jonescb
jonescb

Reputation: 22761

I think this is will do what you want. ModelAdmin.get_urls()
But the problem is going to be, how do you hook up the new url to the admin view?

The default get_urls() is in django/contrib/admin/options.py, and it has some somewhat complex code to generate the default urls.

 def get_urls(self):
    from django.conf.urls.defaults import patterns, url

    def wrap(view):
        def wrapper(*args, **kwargs):
            return self.admin_site.admin_view(view)(*args, **kwargs)
        return update_wrapper(wrapper, view)

    info = self.model._meta.app_label, self.model._meta.module_name

    urlpatterns = patterns('',
        url(r'^$',
            wrap(self.changelist_view),
            name='%s_%s_changelist' % info),
        url(r'^add/$',
            wrap(self.add_view),
            name='%s_%s_add' % info),
        url(r'^(.+)/history/$',
            wrap(self.history_view),
            name='%s_%s_history' % info),
        url(r'^(.+)/delete/$',
            wrap(self.delete_view),
            name='%s_%s_delete' % info),
        url(r'^(.+)/$',
            wrap(self.change_view),
            name='%s_%s_change' % info),
    )
    return urlpatterns

What I would do is use this function in each of your ModelAdmins, but change the url text to your language equivalent.

In your example, add/ would look like this:

url(r'^nuevo/$',
    wrap(self.add_view),
    name='%s_%s_add' % info)

Note that I left %s_%s_add in English.

You can probably wrap this function so you don't have to include the entire thing in each ModelAdmin class.

Edit
That code uses a function named update_wrapper which is imported like this:

from django.utils.functional import update_wrapper

I hadn't seen this function before, and I doubt many people have so I thought it'd be useful to point out the import.

Upvotes: 1

Related Questions