简体   繁体   English

如何在 Django 管理员中更改应用名称?

[英]How to change app name in Django admin?

I created 'frontend' application using ./manage.py startproject frontend我使用./manage.py startproject frontend创建了'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

Yes, you can do it simply是的,你可以简单地做到

in your apps.py file from your app folder change the verbose_name attribute from your config class.在您的 app 文件夹中的apps.py文件中,更改配置类中的 verbose_name 属性。 For example:例如:

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__.py file (from your app)在 Django 3+ 中,您应该将其添加到__init__.py文件中(来自您的应用)

default_app_config = 'frontend.apps.FrontendConfig'

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

I am late but just to add an additional step to the solution explained by @ FACode .我迟到了,但只是为@FACode解释的解决方案添加了一个额外的步骤。
From the offical docs :从官方文档

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',
        # ...
    ]

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, ie the app's views, the urls.py and settings.py files.在它们的依赖项中更改对您的应用程序的任何引用,即应用程序的视图、urls.py 和 settings.py 文件。
  • Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label=''使用以下命令编辑数据库表 django_content_type: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.对于 postgres,使用 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.对于重命名模型,您需要更改 django_content_type.name 注意:如果您的 models.py 的元类列出了 app_name,请确保也将其重命名。

It sounds like you are trying to change how the appname is listed within the Django Admin?听起来您正在尝试更改应用名称在 Django Admin 中的列出方式? This wasn't possible before Django 1.7, but now you can do it like this:这在 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

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/

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 在此处查看相关文档

In apps.py在应用程序.py

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

在 django 3.0+ 中,您只需要更改文件夹名称,然后在 apps.py 中将“( appname )Config”类中的“name”更改为新名称。

For example, if app name is "store" , then add "verbose_name" with "my store" to "store/apps.py" as shown below:例如,如果应用名称“store” ,则将“verbose_name”“my store”添加到“store/apps.py” ,如下所示:

# "store/apps.py"

from django.apps import AppConfig

class StoreConfig(AppConfig):
    name = 'store'
    verbose_name = "my store" # Here

Then, add the code below to "store/ init .py" :然后,将以下代码添加到“store/ init .py”

# "store/__init__.py"

default_app_config = 'store.apps.StoreConfig'

Now, the app name "store" is changed to "my store" as shown below:现在,应用名称“商店”更改为“我的商店” ,如下所示:

在此处输入图像描述

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

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