2017-08-02 21 views
2

的Django允许重写Model,这使我们能够(为Kandydat,而不是默认Kandydats看起来怪异波兰复数例如Kandydaci)指定的Model名字非英语语言正确的复数形式的verbose_nameverbose_name_plural如何使Django管理员在波兰语等语言中使用语法案例?

但是,对于有语法情况的语言而言,这远远不够。例如,Django管理兴高采烈地显示我们类似的东西:

enter image description here

(其中Zaznacz kandydat do zmiany代表Select kandydat to change - KandydatModel的名称)

这是不正确。在这句话中,型号名称应该显示在accusative case,即kandydata。但是,我不能简单地指定的这Modelverbose_nameKandydata,因为也将影响其中nominative case代替预计所有的地方 - 我发现这样一个地方,到目前为止,这是表列的标题:

enter image description here

这是不正确的,因为表标题应该被称为Kandydat而不是Kandydata

如何解决这个问题?

+0

注意:该应用只需要一种语言,即波兰语。 – gaazkam

+0

下面的答案对你的问题有帮助吗? –

+0

@nik_m我很快就会看到它,截至目前,我不得不转移到项目的不同部分,并且必须稍后离开管理员的东西。但几天后我会回到管理员那里,然后我会检查你的答案,我真的很感激。对不起,但我只需要。 – gaazkam

回答

1

字符串'Select <verbose_name> to change'声明内的admin's main view

self.title = title % force_text(self.opts.verbose_name)

作为{{ title }}模板变量

{% block content_title %}{% if title %}<h1>{{ title }}</h1>{% endif %}{% endblock %}

因此,触摸内置视图是没有疑问的。但还有另一种做法!你会发现,Django Admin模板充满了{% block %}作为占位符来覆盖它们。有关overriding Admin templates的官方文档,请阅读更多内容。

所以,对你的问题。

  1. 在您的项目的根目录下创建一个目录templates(如果它不存在)。
  2. 根据my_project/templates/创建另一个admin
  3. my_project/templates/admin/下创建文件change_list.html
  4. my_project/templates/admin/change_list.html把这些:

    {% extends 'admin/change_list.html' %} 
    
    {% load myutils_filters %} 
    
    {% comment %} 
    You have to create an app that will hold all common tags/filters 
    that are re-usable across your entire project. 
    The model class itself is passed as cl.model 
    {% endcomment %} 
    
    {% block content_title %}{{ block.super|to_accusative:cl.model }}{% endblock %} 
    
  5. 里面你myutils/templatetags/myutils_filters.py文件把这些:

    from django import template 
    from django.utils.html import format_html 
    from django.utils.encoding import force_text 
    
    register = template.Library() 
    
    @register.filter() 
    def to_accusative(value, model): 
        verbose_name = model._meta.verbose_name # declared in Meta 
        new_name = force_text(model.accusative case()) # a custom class method (lives in your Model) 
        return format_html(value.replace(verbose_name, new_name)) 
    
  6. 最后,在你的应用程序的models.pyclass这样定义一个classmethod方法每个模型下:

    from django.db import models 
    from django.utils.translation import ugettext_lazy as _ 
    
    class MyModel(models.Model): 
        # model fields here 
    
        def __str__(): 
         return self.a_field 
    
        class Meta: 
         verbose_name = 'verbose name' 
         verbose_name_plural = 'verbose name plural' 
         # accusative_case = 'accusative name case' # BAD. Raises error. Implement a class method intead ;) 
    
        @classmethod 
        def accusative_case(cls): 
         # You'll define the Polish translation as soon as you run makemessages 
         return _('Name in english of the accusative case') 
    
  7. 运行manage.py makemessagesmanage.py compilemessages,重新载入您的浏览器和瞧!

注:以上可能看起来有点hackish的但它的作品辉煌。测试本地和工作。

相关问题