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

news/2024/7/6 1:19:17

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:

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

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


http://www.niftyadmin.cn/n/3648256.html

相关文章

阿里巴巴十年Java架构师分享

1.源码分析专题详细介绍源码中所用到的经典设计思想,看看大牛是如何写代码的,提升技术审美、提高核心竞争力。 帮助大家寻找分析源码的切入点,在思想上来一次巨大的升华。知其然,并知其所以然。把知识变成自己的 2.分布式架构互联…

获取两个时间之间的所有年月份

/** * 获取两个时间之间的所有年月份 * param begDate 格式&#xff1a;yyyy-MM * param endDate 格式&#xff1a;yyyy-MM * return * throws ParseException */ public static List<String> getMonthBetweenTime(String begDate,S…

《职场》笔记20061123

“人生一世&#xff0c;作选择确实是最难的事情&#xff0c;在无法选择时&#xff0c;你不必考虑太多&#xff1b;而选择多了&#xff0c;是好事&#xff0c;但也会带来很多忧虑和挫折&#xff0c;你看&#xff0c;一个两岁的孩子&#xff0c;选择起来很简单&#xff0c;因为他…

阿里架构师和你聊聊【系统架构】

黄勇&#xff0c;从事近十年的 JavaEE 应用开发工作&#xff0c;现任阿里巴巴公司系统架构师。对分布式服务架构与大数据技术有深入研究&#xff0c;具有丰富的 B/S 架构开发经验与项目实战经验&#xff0c;擅长敏捷开发模式。国内开源软件推动者之一&#xff0c;Smart Framewo…

《裸阳》对互联网公司对技术人的启示

“族群&#xff0c;长官。人与人之间的合作。索拉利世界已经把它完全抛弃了。那是一个由孤零零的个人所形成的世界&#xff0c;他们唯一的社会学家对这种情况还感到高兴。少了人与人的相互关系&#xff0c;生命中的主要乐趣就不存在了&#xff0c;智慧的价值也没有了&#xff0…

如何在Ubuntu 20.04上安装和保护phpMyAdmin

An earlier version of this tutorial was written by Brennan Bearnes. 该教程的早期版本由Brennan Bearnes编写 。 介绍 (Introduction) While many users need the functionality of a database management system like MySQL, they may not feel comfortable interacting …

{QA}去新浪好还是去X虎好以及事业起步时什么较重要?

一&#xff1a;这篇日志是针对新近毕业的学生说的&#xff0c;对已经工作许久的人们&#xff0c;也许也有一点点启发意义。二&#xff1a;和新近毕业研究生的对话你说去新浪好还是去X虎好&#xff1f;恩&#xff0c;现在对前途看得不是很清楚啊&#xff0c;比较迷茫。觉得不进百…

《职场》笔记20061207

"非要争论在美国设厂好不好没有多大意义&#xff0c;因为单凭争论本身永远不会出结果&#xff0c;不如我一定要把它做出一个结果。就像德鲁克说的&#xff0c;管理本质不在于知而在于行&#xff0c;不在于逻辑而在于结果。你的逻辑对&#xff0c;我的结果对了&#xff0c;…