AI智能
改变未来

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应用程序中为

Posts

Comments

数据创建了数据库模型。

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:

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

  • You have Django version 3 or higher installed.

    您已安装Django版本3或更高版本。

  • You have connected your Django app to a database. We are using MySQL, and you can achieve this connection by following part two of the Django series, “How To Create a Django App and Connect it to a Database.”

    您已将Django应用程序连接到数据库。 我们正在使用MySQL,您可以通过遵循Django系列的第二部分“ 如何创建Django应用并将其连接到数据库 ”来实现此连接。

  • You are working with a Unix-based operating system, preferably an Ubuntu 20.04 cloud server as this is the system we have tested on. If you would like to set up Django on a similar environment, please refer to our tutorial, “How To Install Django and Set Up a Development Environment on Ubuntu 20.04.

    您正在使用基于Unix的操作系统,最好是Ubuntu 20.04云服务器,因为这是我们经过测试的系统。 如果您想在类似的环境中设置Django,请参考我们的教程“ 如何在Ubuntu 20.04上安装Django和设置开发环境” 。

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 definitionINSTALLED_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中,您可以通过键入

CTRL

X
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 adminfrom django.urls import pathurlpatterns = [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.py

urls.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

命令时,我们应该收到与以下类似的输出。

OutputOperations to perform:Apply all migrations: admin, auth, blogsite, contenttypes, sessionsRunning 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.

打开一个新的终端以连接到服务器,或者通过按

CTRL

C

禁用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

[email protected]

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

,电子邮件

[email protected]

和密码

admin123

创建一个管理员帐户。 您应该根据自己的喜好填写此信息,并确保使用会记住的安全密码。

OutputUsername (leave blank to use \'root\'): admin_userEmail address: [email protected]

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确认密码。

OutputPassword: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

.

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

Post

Comment

添加并注册它。

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 pathfrom . import viewsurlpatterns = [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步-将博客应用连接到管理员 (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

链接。 现在,仪表板当前仅显示

Groups

Users

链接。

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 adminfrom blogsite.models import Postfrom blogsite.models import Commentadmin.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.

现在,您已经在管理面板中注册了

Post

Comment

模型。 这将使管理界面能够选择这些模型并将其显示给已登录并查看管理仪表板的用户。

步骤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管理仪表盘。

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.

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

Post

Comment

模型。

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启用超级管理员

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » django启用超级管理员_如何启用和连接Django管理界面