Django 创建超级用户 (如何在 Django 中高效地创建超级用户)30

作者 : 慕源网 本文共9470个字,预计阅读时间需要24分钟 发布时间: 2021-11-23 共172人阅读

本文是Django教程的一部分,您可以点击Django教程系列查看所有教程。

Django 创建超级用户 (如何在 Django 中高效地创建超级用户)30

Django 介绍创建超级用户

下面的文章提供了 Django 创建超级用户的大纲。从管理员的角度来看,一切都可以在 Django 中轻松处理。此外,Django 框架本身提供了一个管理面板。这个管理面板可以被复杂地处理来管理与站点对应的管理员和其他用户。所以在Django中可以有单独设置超级用户的过程。这个过程是通过Django创建超级用户过程来实现的。此过程涉及创建超级用户表,然后在其中生成超级用户。这就是 Django 中超级用户创建过程的处理方式。

语法:

Python manage.py createsuperuser

上述命令用于设置超级用户创建过程。上面的面板将涉及在后端声明的表数据库上创建超级用户。上面显示的命令如下所示。首先,python 关键字用于创建 python 设置,然后 manage.py 关键字允许在后端的超级用户表中为当前项目启动超级用户设置过程。最后,创建超级用户是实例化创建的命令。

创建超级用户是如何工作的?

  • 执行超级用户创建命令,上述语法中的命令用于超级用户创建。
    Python manage.py runserver
  • 将请求超级用户的用户名,我们需要在此处为​​该项目设置超级用户值。超级用户的用户名将是预期的超级用户值。
  • 接下来,需要提及电子邮件 ID。这是将在后端建立用户 id 和电子邮件 id 中的映射的过程。换句话说,在这种情况下,用户 ID 将被标记为电子邮件 ID。
  • 最后,需要输入密码并与用户确认。此过程允许您输入密码并获取密码以与用户确认。这就是在 Django 中执行超级用户设置过程的方式。

以下日志显示了管理员用户设置过程:

代码:

C:\Users\ANAND\Desktop\ANAND\Websites\Matrimony\Matrimony\matrimony_pages\admin.py changed, reloading.
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
July 30, 2021 - 13:54:32
Django version 3.2.3, using settings 'Matrimony.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[30/Jul/2021 13:39:03] "GET /static/admin/img/Groom.jpg HTTP/1.1" 304 0
[30/Jul/2021 13:39:03] "GET /static/admin/img/Register.jpg HTTP/1.1" 304 0
[30/Jul/2021 13:39:08] "GET /admin/ HTTP/1.1" 302 0
[30/Jul/2021 13:39:08] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 2615
[30/Jul/2021 13:39:08] "GET /static/admin/css/Formpage.css HTTP/1.1" 304 0

创建快照:

Django 创建超级用户 (如何在 Django 中高效地创建超级用户)30

Django 创建超级用户的例子

下面给出的是提到的例子:

Models.py文件

a. (models.py):

代码:

from django.db import models
from django.contrib.auth.models import User
# Model variables
# Create your models here.
class Bride(models.Model):
Super_user_example_name = models.CharField(max_length=200,null=True)
Super_user_example_age = models.IntegerField(null=True)
Super_user_example_thegai = models.CharField(max_length=200,null=True)
Super_user_example_State = models.CharField(max_length=50,null=True)
Super_user_example_District = models.CharField(max_length=50,null=True)
Super_user_example_Address = models.TextField(null=True)
Super_user_example_Phone = models.BigIntegerField(null=True)
Super_user_example_profession = models.CharField(max_length=200,null=True)
Super_user_example_salary = models.BigIntegerField(null=True)
Super_user_example_Under_Graduation_Degree = models.CharField(max_length=200,null=True)
Super_user_example_Under_Graduation_college = models.CharField(max_length=400,null=True)
Super_user_example_Post_Graduation_Degree = models.CharField(max_length=200,null=True)
Super_user_example_Post_Graduation_college = models.CharField(max_length=400,null=True)
Super_user_example_Rasi = models.CharField(max_length=200,null=True)
Super_user_example_Nakshatra = models.CharField(max_length=200,null=True)
Image = models.ImageField(null=True,blank=True,upload_to="img/%y")
def __str__(self):
return self.name

b. (Settings.py):

代码:

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ROOT_URLCONF = 'Matrimony.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [Template_DIR,],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
STATIC_URL = '/static/'
MEDIA_URL = '/images/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'), ] print(STATICFILES_DIRS)
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')
print(STATIC_ROOT)

c. url.py:

代码:

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from matrimony_pages import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'Jsoncheck/',views.Json_Response,name='Json_Response'),
url(r'^$',views.Welcome_page,name='Welcome_page'),
url(r'Mainpage/',views.Main_page,name='Main_page'),
url(r'all/',views.All_users,name='all'),
url(r'form/',views.form_view,name='form_view'),
url(r"signup/", views.Sign_up_request, name="register"),
url(r"login/", views.login_request, name="login"),
path(r'profile/<str:pk>/',views.profile_page,name='profile'),
url(r'logout/',views.logout_request,name='logout'),
url(r'reg/',views.profile_reg_user,name='reg'),
path(r'update/<str:pk>/',views.form_update,name='update'),
path('admin/', admin.site.urls),
url(r'^$',views.Welcome_page,name='Welcome_page'),
url(r'Mainpage/',views.Main_page,name='Main_page'),
url(r'all/',views.All_users,name='all'),
url(r'form/',views.form_view,name='form_view'),
url(r"signup/", views.Sign_up_request, name="register"),
url(r"login/", views.login_request, name="login"),
path(r'profile/<str:pk>/',views.profile_page,name='profile'),
url(r'logout/',views.logout_request,name='logout'),
url(r'reg/',views.profile_reg_user,name='reg'),
path(r'update/<str:pk>/',views.form_update,name='update'),
]

d. views.py:

代码:

@login_required
def Super_user_example_page(request,pk):
Super_user_dict = {}
Super_user_details = Bride.objects.get(id=pk)
Super_user_example_name = Super_user_details.name
Super_user_example_Age = Super_user_details.age
Super_user_example_Thegai = Super_user_details.thegai
Super_user_example_state = Super_user_details.State
Super_user_example_district = Super_user_details.District
Super_user_example_Address = Super_user_details.Address
Super_user_example_Phone = Super_user_details.Phone
Super_user_example_Profession = Super_user_details.profession
Super_user_example_Salary = Super_user_details.salary
Super_user_example_UG = Super_user_details.Under_Graduation_Degree
Super_user_example_UGC = Super_user_details.Under_Graduation_college
Super_user_example_PG = Super_user_details.Post_Graduation_Degree
Super_user_example_PGC = Super_user_details.Post_Graduation_college
Super_user_example_UG = Super_user_details.Under_Graduation_Degree
Super_user_example_UGC = Super_user_details.Under_Graduation_college
Super_user_example_PG = Super_user_details.Post_Graduation_Degree
Super_user_example_PGC = Super_user_details.Post_Graduation_college
Super_user_example_Rasi = Super_user_details.Rasi
Super_user_example_Nakshatra = Super_user_details.Nakshatra
Super_user_example_Image = Super_user_details.Image
Super_user_dict['Age'] = Super_user_example_Age
Super_user_dict['name'] = Super_user_example_name
Super_user_dict['thegai'] = Super_user_example_Thegai
Super_user_dict['State'] = Super_user_example_state
Super_user_dict['district'] = Super_user_example_district
Super_user_dict['Address'] = Super_user_example_Address
Super_user_dict['Phone'] = Super_user_example_Phone
Super_user_dict['profession'] = Super_user_example_Profession
Super_user_dict['Under_Graduation_Degree'] = Super_user_example_UG
Super_user_dict['Under_Graduation_college'] = Super_user_example_UGC
Super_user_dict['Post_Graduation_Degree'] = Super_user_example_PG
Super_user_dict['Post_Graduation_college'] = Super_user_example_PGC
Super_user_dict['Rasi'] = Super_user_example_Rasi
Super_user_dict['Nakshatra'] = Super_user_example_Nakshatra
Super_user_dict['Image'] = Super_user_example_Image
print(Super_user_details.Creator)
print(Super_user_dict)
return render(request,'Profilepage.html',Super_user_dict)

e. Profilepage.html:

代码:

<!DOCTYPE html>
<html style="font-size: 16px;">
<head>
<title>Home</title>
{% load static %}
<link rel="stylesheet" href="{% static 'admin/css/Mainpage.css'%}" media="screen">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1" />
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<!-- Script -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="body">
<img class="img" src="{% static 'admin/img/Wedding1.jpg' %}" >
<nav class='navbar'>
<div class='navbar_div'>
<a class="navbar" onclick="redirect2()" >Home! </a>
<a class="navbar" onclick="redirect2()" >Contact</a>
</div>
</nav>
<form method="GET" action=".">
<div class="form-row">
<div class="form-group col-12">
<div class="input-group">
<input class="form-control py-2 border-right-0 border" type="search" name="thegai" placeholder="thegai..." />
<input class="form-control py-2 border-right-0 border" list="Rasi" type="search" name="Rasi" placeholder="Rasi..." />
<span class="input-group-append">
<div class="input-group-text bg-transparent">
<i class="fa fa-search"></i>
</div>
</span>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Apply filters</button>
<button type="submit" onclick=redirect1() class="btn btn-primary">Remove All filters</button>
</form>
<div class="container">
<br>
{% block content %}
<div class="row">
{% for bride in brides %}
<div class="col-lg-4">
{% if bride.Image %}
<img src="{{bride.Image.url}}" align="right" alt="Image" class="img-thumbnail" style="max-height:92px">
{% else %}
<img src="{% static 'admin/img/default.png' %}" align="right" alt="Image" class="img-thumbnail" style="max-height:92px">
{% endif%}
<div class="box-element">
<h5><strong>{{bride.name}}</strong></h5>
<h6>{{bride.age}}, {{bride.thegai}}, {{bride.State}}</h6>
<form action="" method="POST">
{% csrf_token %}
<a class="btn btn-sm btn-info" href="{% url 'profile' bride.id %}" name="{{bride.id}}"><strong>View profile</strong></a>
</form>
</div>
<br></br>
</div>
<br>
{% endfor %}
{% endblock content %}
</div>
</body>
</html>

输出:

Django 创建超级用户 (如何在 Django 中高效地创建超级用户)30

结论

这篇文章描述了如何在 Django 中高效地创建超级用户。此外,在超级用户创建的每个步骤中,这些更改都用合适的示例进行了解释。此外,还创建了超级用户并显示了页面的输出。


慕源网 » Django 创建超级用户 (如何在 Django 中高效地创建超级用户)30

常见问题FAQ

程序仅供学习研究,请勿用于非法用途,不得违反国家法律,否则后果自负,一切法律责任与本站无关。
请仔细阅读以上条款再购买,拍下即代表同意条款并遵守约定,谢谢大家支持理解!

发表评论

开通VIP 享更多特权,建议使用QQ登录