Django 管理站点重命名 URL 以匹配我的语言

发布于 2024-11-03 14:52:43 字数 71 浏览 3 评论 0原文

我想自定义管理站点的 URL,以便它们以我的母语(西班牙语)显示。我想要的是使用 /nuevo 而不是 /add 等。这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-11-10 14:52:43

我想这会做你想做的。 ModelAdmin.get_urls()
但问题是,如何将新的 url 连接到 admin 视图?

默认的 get_urls() 位于 django/contrib/admin/options.py 中,它有一些有点复杂的代码来生成默认的 url。

 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'^

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:

url(r'^nuevo/

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

from django.utils.functional import update_wrapper

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.changelist_view), name='%s_%s_changelist' % info), url(r'^add/

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:


请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:


我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

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

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:


请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:


我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.history_view), name='%s_%s_history' % info), url(r'^(.+)/delete/

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:


请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:


我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.delete_view), name='%s_%s_delete' % info), url(r'^(.+)/

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:


请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:


我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.change_view), name='%s_%s_change' % info), ) return urlpatterns

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:


请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:


我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.add_view), name='%s_%s_add' % info)

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.changelist_view), name='%s_%s_changelist' % info), url(r'^add/

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

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

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.history_view), name='%s_%s_history' % info), url(r'^(.+)/delete/

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.delete_view), name='%s_%s_delete' % info), url(r'^(.+)/

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

, wrap(self.change_view), name='%s_%s_change' % info), ) return urlpatterns

我要做的就是在每个 ModelAdmin 中使用此函数,但将 url 文本更改为您的语言等效项。

在您的示例中,add/ 如下所示:

请注意,我将 %s_%s_add 保留为英语。

您可以包装此函数,这样您就不必在每个 ModelAdmin 类中包含整个函数。

编辑
该代码使用了一个名为 update_wrapper 的函数,该函数的导入方式如下:

我以前没有见过这个函数,而且我怀疑很多人都见过,所以我认为指出导入会很有用。

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'^

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/

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.

, wrap(self.changelist_view), name='%s_%s_changelist' % info), url(r'^add/

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:


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:


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.

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

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:


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:


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.

, wrap(self.history_view), name='%s_%s_history' % info), url(r'^(.+)/delete/

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:


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:


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.

, wrap(self.delete_view), name='%s_%s_delete' % info), url(r'^(.+)/

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:


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:


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.

, 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:


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:


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.

, 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:

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.

, wrap(self.changelist_view), name='%s_%s_changelist' % info), url(r'^add/

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:

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:

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.

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

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:

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:

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.

, wrap(self.history_view), name='%s_%s_history' % info), url(r'^(.+)/delete/

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:

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:

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.

, wrap(self.delete_view), name='%s_%s_delete' % info), url(r'^(.+)/

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:

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:

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.

, 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:

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文