简体   繁体   English

从模板访问 Django CreateView 中的模型名称

[英]Access model name in Django CreateView from template

I'm using the generic CRUD views in Django 1.6, eg:我在 Django 1.6 中使用通用 CRUD 视图,例如:

class KanriCreateView(CreateView):
    template_name = 'generic_form.html'

class KanriUpdateView(UpdateView):
    template_name = 'generic_form.html'

etc.等等。

NB These are classes used as a base class which I subclass inside views.py files throughout the project.注意这些是用作基类的类,我在整个项目的 views.py 文件中对其进行了子类化。

In order to keep DRY I'm writing a generic form template for all create/update views.为了保持干燥,我正在为所有创建/更新视图编写一个通用表单模板。

For update views, I have access to object in the template, which is the instance I am updating.对于更新视图,我可以访问模板中的object ,即我正在更新的实例。 I then use object.__class__.__name__ (via a custom filter) to get the name of the class (so I can have automatically generated custom buttons like "Add User", "Add Role".etc so the forms look less...generic.然后我使用object.__class__.__name__ (通过自定义过滤器)来获取类的名称(这样我就可以自动生成自定义按钮,如“添加用户”、“添加角色”等,这样表单看起来就少了......通用的。

Of course, when I'm using my template in CreateView , object does not exist (as it has not been created), so my custom buttons.etc do not work, and I get a VariableDoesNotExist exception.当然,当我在CreateView使用我的模板时, object不存在(因为它没有被创建),所以我的自定义按钮等不起作用,我得到一个VariableDoesNotExist异常。

Does Django provide the class somewhere so I can use it in the template? Django 是否在某处提供了以便我可以在模板中使用它?

  1. The name of your first view should be different, eg KanriCreateView您的第一个视图的名称应该不同,例如KanriCreateView
  2. It might help you to get the name of the view class: {{ view.它可能会帮助您获取视图类的名称:{{ view. class . name }}姓名}}
  3. If you have access to the view class (which is provided by default by the ContextDataMixin ) you can access the model attribute of the view class and get the name of the model: {{ view.model.__name__ }}如果您有权访问视图类(默认情况下由ContextDataMixin ),您可以访问视图类的model属性并获取模型的名称: {{ view.model.__name__ }}

Cheers干杯

If you're using a ModelForm for your CreateView, this doesn't quite work.如果您为 CreateView 使用 ModelForm,则这不太有效。 This is because you're not specifying这是因为你没有指定

model = MyModel

but instead you're specifying但相反,您正在指定

form_class = MyModelForm

so what you can do instead is所以你可以做的是

from django.contrib.admin.utils import model_ngettext

model_ngettext(self.form_class._meta.model, 1)

I propose a solution updated for Django 2 and 3: retrieve the model verbose name from the ModelForm associated to the CreateView.我提出了一个针对 Django 2 和 3 更新的解决方案:从与 CreateView 关联的 ModelForm 中检索模型详细名称。

class YourCreateView(CreateView):

    form_class = YourModelForm

    def get_context_data(self, **kwargs):
        """Add the models verbose name to the context dictionary."""

        kwargs.update({
            "verbose_name": self.form_class._meta.model._meta.verbose_name,})
        return super().get_context_data(**kwargs)

Now you can use {{ verbose_name }} inside your template.现在您可以在模板中使用{{ verbose_name }}

Please, remark the double _meta in the code snippet above: the first is meant to access the model from the ModelForm, while the second accesses the verbose name of the Model.请注意上面代码片段中的_meta :第一个用于从 ModelForm 访问模型,而第二个用于访问模型的详细名称。

As with internationalization, remark that if your model uses ugettext as shown below, then the verbose name will automatically be translated in the template.与国际化一样,请注意,如果您的模型使用如下所示的 ugettext,那么详细名称将在模板中自动翻译。

from django.utils.translation import ugettext_lazy as _

class MyModel(models.Model):
    class Meta:
        verbose_name = _("your verbose name")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM