Python 如何在 Django admin 中更改应用程序名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26972625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 01:14:14  来源:igfitidea点击:

How to change app name in Django admin?

pythondjango

提问by

I created 'frontend'application using ./manage.py startproject frontend

我创建的'frontend'应用程序使用./manage.py startproject frontend

But for some reason I just want to change the app name in the Django admin to display 'Your Home Page'instead of 'frontend'.

但出于某种原因,我只想将 Django 管理中的应用程序名称更改为显示'Your Home Page'而不是'frontend'.

How to do that?

怎么做?

Update: Here is the little more detail:

更新:这里是更多的细节:

#settings.py
INSTALLED_APPS = (
    'frontend',
)

and

#frontend/models.py
class Newpage(models.Model):
    #field here
class Oldpage(models.Model):
    #field here

回答by wolendranh

1.Try to add app_label to your model that will be registered in Admin.

1.尝试将 app_label 添加到将在 Admin 中注册的模型。

class MyModel(models.Model):
        pass
    class Meta:
        app_label = 'My APP name'

UPDATE:2. Steps to rename app(folders):

更新:2. 重命名应用程序(文件夹)的步骤:

  • Rename the folder which is in your project root
  • Change any references to your app in their dependencies, i.e. the app's views, the urls.py and settings.py files.
  • Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label=''
  • Also if you have models, you will have to rename the model tables. For postgres use ALTER TABLE _modelName RENAME TO _modelName. For renaming models, you'll need to change django_content_type.name Note: If your models.py 's Meta Class has app_name listed, make sure to rename that too.
  • 重命名项目根目录中的文件夹
  • 在它们的依赖项中更改对您的应用程序的任何引用,即应用程序的视图、urls.py 和 settings.py 文件。
  • 使用以下命令编辑数据库表 django_content_type:UPDATE django_content_type SET app_label='' WHERE app_label=''
  • 此外,如果您有模型,则必须重命名模型表。对于 postgres,使用 ALTER TABLE _modelName RENAME TO _modelName。对于重命名模型,您需要更改 django_content_type.name 注意:如果您的 models.py 的元类列出了 app_name,请确保也将其重命名。

回答by seddonym

It sounds like you are trying to change how the appname is listed within the Django Admin? This wasn't possible before Django 1.7, but now you can do it like this:

听起来您正在尝试更改应用名称在 Django Admin 中的列出方式?这在 Django 1.7 之前是不可能的,但现在你可以这样做:

https://docs.djangoproject.com/en/1.7/ref/applications/#for-application-users

https://docs.djangoproject.com/en/1.7/ref/applications/#for-application-users

回答by FACode

Yes, you can do it simply

是的,你可以简单地做到

in your apps.pyfile from your app folder change the verbose_name attribute from your config class. For example:

在您的 app 文件夹中的apps.py文件中,更改配置类中的 verbose_name 属性。例如:

from django.apps import AppConfig


class FrontendConfig(AppConfig):
    name = 'frontend'
    verbose_name = "Your Home Page"

I test it and works in Django 1.10

我测试它并在 Django 1.10 中工作

UPDATE:

更新:

In Django 3+ you should add this in your __init__.pyfile (from your app)

在 Django 3+ 中,您应该将其添加到您的__init__.py文件中(来自您的应用程序)

default_app_config = 'frontend.apps.FrontendConfig'

回答by Akhilraj N S

In stars/app.py

在星星/app.py

from django.apps import AppConfig


class RequestsConfig(AppConfig):
    name = 'stars'
    verbose_name = "Star of India"

in stars/init.py

在星星/ init.py

default_app_config = 'stars.apps.RequestsConfig'

To access the app name in custom menu methods you can try to get that from

要在自定义菜单方法中访问应用程序名称,您可以尝试从

model._meta.app_config.verbose_name

Check the Django Doc for reference https://docs.djangoproject.com/en/1.11/ref/applications/#for-application-users

检查 Django 文档以供参考https://docs.djangoproject.com/en/1.11/ref/applications/#for-application-users

回答by hanou23

In apps.py

在应用程序.py

 from django.apps import AppConfig
 class AccountConfig(AppConfig):
    name = 'app name'
    verbose_name = "new app name"

回答by Georgie

In Django 2 we can use verbose_name in apps.py but we have to specify the CLass Config in init.py

在 Django 2 中,我们可以在 apps.py 中使用 verbose_name 但我们必须在init.py 中指定 CLass Config

See related doc here

在此处查看相关文档

回答by Alexandre Paes

I am late but just to add an additional step to the solution explained by @FACode.
From the offical docs:

我迟到了,但只是为@FACode解释的解决方案添加了一个额外的步骤。
从官方文档

In frontend/apps.py

在前端/apps.py

from django.apps import AppConfig


class FrontendConfig(AppConfig):
    name = 'frontend'
    verbose_name = "Your Home Page"

In app/settings.py

在 app/settings.py

    INSTALLED_APPS = [
        'frontend.apps.FrontendConfig',
        # ...
    ]

回答by Eliotnand

apps.py

应用程序.py

from django.apps import AppConfig

class FrontendConfig(AppConfig):
    name = 'frontend'
    verbose_name = "Your Home Page"

__ init__.py

__初始化__.py

default_app_config = 'frontend.apps.FrontendConfig'

https://docs.djangoproject.com/en/2.2/ref/applications/

https://docs.djangoproject.com/en/2.2/ref/applications/