django启用超级管理员_如何启用和连接Django管理界面

django启用超级管理员

介绍 (Introduction)

If you have followed along in the Django Development series, you’ve started a Django application, connected your application to MySQL, and created the database models for the Posts and Comments data within your blog web application.

如果您遵循Django Development系列教程 ,那么您已经启动了Django应用程序,将应用程序连接到MySQL,并在博客Web应用程序中为PostsComments数据创建了数据库模型。

In this tutorial, we will connect to and enable the Django admin site so that you can manage your blog website. The Django admin site comes pre-built with a user interface that is designed to allow you and other trusted individuals to manage content for the website.

在本教程中,我们将连接并启用Django管理网站,以便您可以管理博客网站。 Django管理网站预先构建了一个用户界面,该界面旨在使您和其他受信任的个人可以管理网站的内容。

It is worth noting that Django’s official documentation points out that although this is ideal for an organization’s internal use, it is not recommended to build a web application around an automatically generated Django admin interface. If you find that your interface needs to be more process-centric or proves to abstract away the implementation details of database tables and fields, it would be best for you to write your own views for the admin side.

值得注意的是,Django的官方文档指出,尽管这对于组织内部使用而言是理想的选择,但不建议您围绕自动生成的Django管理界面构建Web应用程序。 如果您发现您的界面需要以流程为中心,或者被证明可以抽象出数据库表和字段的实现细节,那么最好为管理员编写自己的视图。

先决条件 (Prerequisites)

This tutorial is part of the Django Development series and is a continuation of that series.

本教程是Django开发系列的一部分,并且是该系列的继续。

If you have not followed along with this series, we are making the following assumptions:

如果您未遵循本系列文章,我们将进行以下假设:

As this tutorial is largely dealing with the Django Admin Interface, you may be able to follow along even if you have a somewhat different setup.

由于本教程主要涉及Django Admin Interface,因此即使设置有所不同,您也可以继续学习。

步骤1 —启用管理员 (Step 1 — Enable the Admin)

Whenever we begin doing work in Python and Django, we should activate our Python virtual environment and move into our app’s root directory. If you followed along with the series, you can achieve this by typing the following.

每当我们开始在Python和Django中进行工作时,都应激活Python虚拟环境并移至应用程序的根目录。 如果按照该系列进行学习,则可以通过键入以下内容来实现。

  • cd ~/my_blog_app

    cd〜/ my_blog_app
  • . env/bin/activate

    。 env / bin / activate

In order to enable the Django Admin, we need to ensure that our app is part of the list of INSTALLED_APPS in the settings.py file.

为了启用Django Admin,我们需要确保我们的应用程序是settings.py文件中INSTALLED_APPS列表的一部分。

Navigate to the directory of the settings file:

导航到设置文件的目录:

  • cd ~/my_blog_app/blog/blog/

    cd〜/ my_blog_app / blog / blog /

From here, open the settings.py file. If it’s not already there, add django.contrib.admin to the list of INSTALLED_APPS, using a text editor like nano.

从这里打开settings.py文件。 如果尚不存在,请使用文本编辑器(例如nano)将django.contrib.admin添加到INSTALLED_APPS列表中。

  • nano settings.py

    纳米settings.py

The INSTALLED_APPS section of the file should be similar to the file below. Our app in the list is the one on the top, 'blogsite', but if you created an app of a different name, ensure that that app is listed in this file as demonstrated.

该文件的INSTALLED_APPS部分应与下面的文件相似。 列表中我们的应用程序是顶部的'blogsite',但是如果您创建了其他名称的应用程序,请确保该应用程序已在演示文件中列出。

settings.py
settings.py
...
# Application definition
INSTALLED_APPS = [
    'blogsite',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]
...

Be sure to save and close the file if you made changes. In nano, you can do this by typing CTRL and X then Y and then ENTER.

如果进行了更改,请确保保存并关闭文件。 在nano中,您可以通过键入CTRLX Y ,然后按ENTER来执行此操作。

We can now open the urls.py file, again with nano or another text editor.

现在,我们可以再次使用nano或其他文本编辑器打开urls.py文件。

  • nano urls.py

    纳米urls.py

Under the comment at the top, the file should resemble the following.

在顶部的注释下,该文件应类似于以下内容。

urls.py
urls.py
…
"""
from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

If the file is different from what is above, copy and paste the lines above into your urls.py file.

如果文件与上面的文件不同,请将上面的行复制并粘贴到urls.py文件中。

Now that we have ensured that our Django web project has the appropriate code in the settings.py and urls.py files, we know our application will have access to the admin models and admin user interface.

现在,我们已经确保Django Web项目在settings.pyurls.py文件中具有适当的代码,我们知道我们的应用程序将有权访问管理模型和管理用户界面。

步骤2 —验证管理员是已安装的应用程序 (Step 2 — Verify that Admin is an Installed App)

We should next migrate the models to the database so that it picks up the newly added Admin models.

接下来,我们应该将模型迁移到数据库中,以便使用新添加的Admin模型。

Navigate to the directory where the manage.py file is located.

导航到manage.py文件所在的目录。

  • cd ~/my_blog_app/blog

    cd〜/ my_blog_app / blog

Remember to run the migrate command whenever you make any changes to the models, like so.

记住,对models进行任何更改时都应运行migrate命令,就像这样。

  • python manage.py migrate

    python manage.py迁移

If we did not make any changes to the files above, we should receive output similar to the following upon running the migrate command.

如果我们未对上面的文件进行任何更改,则在运行migrate命令时,我们应该收到与以下类似的输出。


   
   
Output
Operations to perform: Apply all migrations: admin, auth, blogsite, contenttypes, sessions Running migrations: No migrations to apply.

Otherwise, the output should indicate that Django made the migrations needed to support our app.

否则,输出应表明Django进行了支持我们应用程序的迁移。

We can now start the server by running the following command. You can replace 0.0.0.0 with your IP address.

现在,我们可以通过运行以下命令来启动服务器。 您可以用IP地址替换0.0.0.0

  • python manage.py runserver 0.0.0.0:8000

    python manage.py runserver 0.0.0.0:8000

Then navigate to the admin panel’s URL in a browser of your choice. Be sure to input your server’s IP address.

然后在您选择的浏览器中导航到管理面板的URL。 确保输入服务器的IP地址。

http://your-server-ip:8000/admin/

You will receive a login screen similar to this.

您将收到与此类似的登录屏幕。

Getting to this screen lets us know that we have successfully enabled the admin app.

进入此屏幕可让我们知道我们已成功启用管理应用程序。

Though we have enabled the app, we may not have set up a Django administration account yet. We can create the admin account in order to login in the next step.

尽管已经启用了该应用程序,但我们可能尚未设置Django管理帐户。 我们可以创建管理员帐户以便在下一步中登录。

第3步-创建管理员超级用户帐户 (Step 3 — Create Admin Super-User Account)

If you already have set up an admin account and can log into your admin page, you can skip this step.

如果您已经设置了管理员帐户并可以登录到管理员页面,则可以跳过此步骤。

Open a new terminal to connect to the server, or disable the Django app by pressing CTRL and C so that we can work on our server terminal’s programming environment.

打开一个新的终端以连接到服务器,或者通过按CTRLC禁用Django应用,以便我们可以在服务器终端的编程环境中工作。

Django allows you to generate a super-user account, which we can do by running the manage.py file to start the super-user creation process.

Django允许您生成一个超级用户帐户,我们可以通过运行manage.py文件来启动超级用户创建过程。

  • python manage.py createsuperuser

    python manage.py createsuperuser

Once we do so, we’ll be prompted to fill in details for our username, email, and password. In this tutorial, we’ll make an admin account with the username admin_user, the email sammy@example.com and the password admin123. You should fill this information in with your own preferences and be sure to use a secure password that you’ll remember.

完成后,系统将提示我们填写用户名,电子邮件和密码的详细信息。 在本教程中,我们将使用用户名admin_user ,电子邮件sammy@example.com和密码admin123创建一个管理员帐户。 您应该根据自己的喜好填写此信息,并确保使用会记住的安全密码。


   
   
Output
Username (leave blank to use 'root'): admin_user Email address: sammy@example.com

Then put in your password twice when you see the Password: prompt. You will not receive output from the keystrokes of your password when you enter it. Press enter after each prompt to confirm your password.

然后,在看到“ Password:提示时, Password:两次Password: 。 输入密码时,您将不会收到密码击键的输出。 在每个提示后按Enter确认密码。


   
   
Output
Password: Password (again):

At this point, we now have an admin account with the username admin_user and the password admin123.

至此,我们现在有了一个管理员帐户,名称为admin_user ,密码为admin123

Let’s log in and investigate what exists on our admin page.

让我们登录并调查管理页面上存在的内容。

If needed, run the Django app again with python manage.py runserver 0.0.0.0:8000 and then navigate once more to the URL http://your-server-ip:8000/admin/ to get to the admin login page. Then log in with the username and password and password you just created.

如果需要,请使用python manage.py runserver 0.0.0.0:8000再次运行Django应用,然后再次导航至URL http:// your-server-ip :8000/admin/以进入管理员登录页面。 然后使用您刚创建的用户名,密码和密码登录。

After a successful login, you’ll receive the following page.

成功登录后,您将收到以下页面。

Next, we will need to work on connecting our blog app to the admin panel.

接下来,我们将需要将博客应用程序连接到管理面板。

第4步-创建用于发布和评论的URL模式 (Step 4 — Create URL Patterns for Post and Comment)

In the previous step, we successfully logged into the admin interface, but you may have noticed that our blog app is not yet available there. To populate our admin interface with the blog app, we need to add and register it with the associated models Post and Comment.

在上一步中,我们成功登录了管理界面,但是您可能已经注意到我们的博客应用尚不可用。 要使用博客应用填充我们的管理界面,我们需要使用关联的模型PostComment添加并注册它。

To do this, we’ll create an empty file called urls.py, in the blogsite directory, like so:

为此,我们将在blogsite目录中创建一个名为urls.py的空文件,如下所示:

  • touch ~/my_blog_app/blog/blogsite/urls.py

    触摸〜/ my_blog_app / blog / blogsite / urls.py

In this file, we will add the URL pattern for our blog application so that we can access it via the admin interface.

在此文件中,我们将为博客应用程序添加URL模式,以便我们可以通过管理界面访问它。

Navigate to the location of that urls.py file we’ve just created.

导航到我们刚刚创建的urls.py文件的位置。

  • cd ~/my_blog_app/blog/blogsite/

    cd〜/ my_blog_app / blog / blogsite /

Then open the file with nano, for instance.

然后,例如,使用nano打开文件。

  • nano urls.py

    纳米urls.py

Add the following lines of code to the file.

将以下代码行添加到文件中。

urls.py
urls.py
from django.urls import path
from . import views
urlpatterns = [
    path('$/', views.posts, name='posts'),
    path('$/', views.comments, name='comments'),
]

These are the URL pattern expressions needed to allow our application to access the views for Posts and Comments. We have not created those views yet, but will cover this later on in the series.

这些是URL模式表达式,允许我们的应用程序访问Posts and Comments views 。 我们尚未创建这些views ,但是将在本系列的后面部分进行介绍。

第5步-将Blog应用连接到管理员 (Step 5 — Connect the Blog App to Admin)

Connecting our blog to the admin interface will allow us to see links for both the Posts and Comments inside the admin dashboard. Right now, the dashboard currently just displays links for Groups and Users.

将我们的博客连接到管理界面将使我们能够在管理仪表板内看到“ Posts和“ Comments链接。 现在,仪表板当前仅显示GroupsUsers链接。

To connect the two together, we need to register our Posts and Comments models inside of the admin file of blogsite.

要将两者连接在一起,我们需要在blogsite的管理文件中注册我们的Posts and Comments模型。

Navigate to the blogsite directory:

导航到blogsite目录:

  • cd ~/my_blog_app/blog/blogsite

    cd〜/ my_blog_app / blog / blogsite

Then, open the admin.py file in a text editor of your choice.

然后,在您选择的文本编辑器中打开admin.py文件。

  • nano admin.py

    纳米管理员

The file will be populated with an import statement and a comment.

该文件将使用导入语句和注释填充。

admin.py
管理员
from django.contrib import admin

# Register your models here.

You should edit the file so that it contains the following code in order to support our app.

您应该编辑该文件,使其包含以下代码,以支持我们的应用程序。

admin.py
管理员
from django.contrib import admin
from blogsite.models import Post
from blogsite.models import Comment


admin.site.register(Post)
admin.site.register(Comment)

When you are satisfied with the file, save and exit.

对文件满意后,保存并退出。

You have now registered the Post and Comment models inside of the admin panel. This will enable the admin interface to pick these models up and show them to the user that is logged into and viewing the admin dashboard.

现在,您已经在管理面板中注册了PostComment模型。 这将使管理界面能够选择这些模型并将其显示给已登录并查看管理仪表板的用户。

步骤6 —确认Blog App已添加到管理员 (Step 6 — Verify that Blog App has Been Added to Admin)

Now that you’ve added the relevant Python code, run the server. Open http://your-server-ip:8000/admin and log in to the admin using your credentials if you’re not logged in already. In this tutorial, we’ve been logging in with the username admin_user and password admin123.

现在,您已经添加了相关的Python代码,请运行服务器。 如果您尚未登录, http:// your-server-ip :8000/admin打开http:// your-server-ip :8000/admin并使用您的凭据登录到admin。 在本教程中,我们使用用户名admin_user和密码admin123登录。

Now that you’ve logged in, you should be served the following webpage. If it has not changed from before, you may need to refresh your browser.

现在,您已经登录,应该为您提供以下网页。 如果以前没有更改,则可能需要刷新浏览器。

This verifies that we have now connected our app, blogsite, to the Django admin dashboard.

这验证我们现在已经将我们的应用blogsite连接到Django admin仪表盘。

When you are done with testing your app, you can press CTRL + C to stop running the Django server. This will return you to your programming environment.

测试完应用程序后,可以按CTRL + C停止运行Django服务器。 这将使您返回到编程环境。

When you are ready to leave your Python environment, you can run the deactivate command:

当您准备离开Python环境时,可以运行deactivate命令:

  • deactivate

    停用

Deactivating your programming environment will put you back to the terminal command prompt.

停用编程环境将使您返回到终端命令提示符。

结论 (Conclusion)

In this tutorial, you have successfully enabled the admin interface, created an admin login, and registered the Post and Comment models with the admin.

在本教程中,您已经成功启用了管理员界面,创建了管理员登录名,并向管理员注册了PostComment模型。

The Django admin interface is how you will be able to create posts and monitor comments with your blog.

Django管理界面是您能够使用博客创建帖子和监视评论的方式。

Coming up in the series, we will be creating the views for the blog application.

在本系列中,我们将为博客应用程序创建views

翻译自: https://www.digitalocean.com/community/tutorials/how-to-enable-and-connect-the-django-admin-interface

django启用超级管理员

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值