脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Django实现前后端登录

Django实现前后端登录

2021-10-24 10:17gexzygg Python

这篇文章主要介绍了Django实现前后端登录的示例,帮助大家更好的理解和学习使用Django,感兴趣的朋友可以了解下

前端登录

1. login.vue

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
<template>
    <div class="sign">
    <div class="logo"><a href="/" rel="external nofollow" ><img src="/static/image/nav-logo.png" alt="logo"></a></div>
    <div class="main">
 
 
<h4 class="title">
  <div class="normal-title">
    <a class="active" href="/login" rel="external nofollow" >登录</a>
    <b>·</b>
    <a id="js-sign-up-btn" class="" href="/register" rel="external nofollow" >注册</a>
  </div>
</h4>
<div class="js-sign-in-container">
  <form id="new_session" action="" method="post">
      <div class="input-prepend restyle js-normal">
        <input placeholder="手机号或邮箱" type="text" name="session[email_or_mobile_number]" id="session_email_or_mobile_number">
        <i class="iconfont ic-user"></i>
      </div>
    <!-- 海外登录登录名输入框 -->
 
    <div class="input-prepend">
      <input placeholder="密码" type="password" name="password" id="session_password">
      <i class="iconfont ic-password"></i>
    </div>
    <div class="remember-btn">
      <input type="checkbox" value="true" checked="checked" name="remember_me" id="session_remember_me"><span>记住我</span>
    </div>
    <div class="forget-btn">
      <a class="" data-toggle="dropdown" href="">登录遇到问题?</a>
    </div>
    <button class="sign-in-button" id="sign-in-form-submit-btn" type="button">
      <span id="sign-in-loading"></span>
      登录
    </button>
</form>
  <!-- 更多登录方式 -->
  <div class="more-sign">
    <h6>社交帐号登录</h6>
    <ul>
  <li id="weibo-link-wrap" class="">
    <a class="weibo" id="weibo-link">
      <i class="iconfont ic-weibo"></i>
    </a>
  </li>
  <li><a id="weixin" class="weixin" target="_blank" href=""><i class=" rel="external nofollow"  rel="external nofollow" iconfont ic-wechat"></i></a></li>
  <li><a id="qq" class="qq" target="_blank" href=""><i class=" rel="external nofollow"  rel="external nofollow" iconfont ic-qq_connect"></i></a></li>
</ul>
  </div>
</div>
 
    </div>
  </div>
</template>
 
<script>
    export default {
        name: "login"
    }
</script>
 
<style scoped>
input{
  outline: none;
}
*, :after, :before {
    box-sizing: border-box;
}
.sign {
    height: 100%;
    min-height: 750px;
    text-align: center;
    font-size: 14px;
    background-color: #f1f1f1
}
 
.sign:before {
    content: "";
    display: inline-block;
    height: 85%;
    vertical-align: middle
}
 
.sign .disable,.sign .disable-gray {
    opacity: .5;
    pointer-events: none
}
 
.sign .disable-gray {
    background-color: #969696
}
 
.sign .tooltip-error {
    font-size: 14px;
    line-height: 25px;
    white-space: nowrap;
    background: none
}
 
.sign .tooltip-error .tooltip-inner {
    max-width: 280px;
    color: #333;
    border: 1px solid #ea6f5a;
    background-color: #fff
}
 
.sign .tooltip-error .tooltip-inner i {
    position: static;
    margin-right: 5px;
    font-size: 20px;
    color: #ea6f5a;
    vertical-align: middle
}
 
.sign .tooltip-error .tooltip-inner span {
    vertical-align: middle;
    display: inline-block;
    white-space: normal;
    max-width: 230px
}
 
.sign .tooltip-error.right .tooltip-arrow-border {
    border-right-color: #ea6f5a
}
 
.sign .tooltip-error.right .tooltip-arrow-bg {
    left: 2px;
    border-right-color: #fff
}
 
.sign .slide-error {
    position: relative;
    padding: 10px 0;
    border: 1px solid #c8c8c8;
    border-radius: 4px
}
 
.sign .slide-error i {
    position: static!important;
    margin-right: 10px;
    color: #ea6f5a!important;
    vertical-align: middle
}
 
.sign .slide-error span {
    font-size: 15px;
    vertical-align: middle
}
 
.sign .slide-error div {
    margin-top: 10px;
    font-size: 13px
}
 
.sign .slide-error a {
    color: #3194d0
}
 
.sign .js-sign-up-forbidden {
    color: #999;
    padding: 80px 0 100px
}
 
.sign .js-sign-up-container .slide-error {
    border-bottom: none;
    border-radius: 0
}
 
.sign .logo {
    position: absolute;
    top: 56px;
    margin-left: 50px
}
 
.sign .logo img {
    width: 100px
}
 
.sign .main {
    width: 400px;
    margin: 60px auto 0;
    padding: 50px 50px 30px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 0 8px rgba(0,0,0,.1);
    vertical-align: middle;
    display: inline-block
}
 
.sign .reset-title,.sign .title {
    margin: 0 auto 50px;
    padding: 10px;
    font-weight: 400;
    color: #969696
}
 
.sign .reset-title a,.sign .title a {
    padding: 10px;
    color: #969696
}
 
.sign .reset-title a:hover,.sign .title a:hover {
    border-bottom: 2px solid #ea6f5a
}
 
.sign .reset-title .active,.sign .title .active {
    font-weight: 700;
    color: #ea6f5a;
    border-bottom: 2px solid #ea6f5a
}
 
.sign .reset-title b,.sign .title b {
    padding: 10px
}
 
.sign .reset-title {
    color: #333;
    font-weight: 700
}
 
.sign form {
    margin-bottom: 30px
}
 
.sign form .input-prepend {
    position: relative;
    width: 100%
}
 
.sign form .input-prepend input {
    width: 100%;
    height: 50px;
    margin-bottom: 0;
    padding: 4px 12px 4px 35px;
    border: 1px solid #c8c8c8;
    border-radius: 0 0 4px 4px;
    background-color: hsla(0,0%,71%,.1);
    vertical-align: middle
}
 
.sign form .input-prepend i {
    position: absolute;
    top: 14px;
    left: 10px;
    font-size: 18px;
    color: #969696
}
 
.sign form .input-prepend span {
    color: #333
}
 
.sign form .input-prepend .ic-show {
    top: 18px;
    left: auto;
    right: 8px;
    font-size: 12px
}
 
.sign form .geetest-placeholder {
    height: 44px;
    border-radius: 4px;
    background-color: hsla(0,0%,71%,.1);
    text-align: center;
    line-height: 44px;
    font-size: 14px;
    color: #999
}
 
.sign form .restyle {
    margin-bottom: 0
}
 
.sign form .restyle input {
    border-bottom: none;
    border-radius: 4px 4px 0 0
}
 
.sign form .no-radius input {
    border-radius: 0
}
 
.sign form .slide-security-placeholder {
    height: 32px;
    background-color: hsla(0,0%,71%,.1);
    border-radius: 4px
}
 
.sign form .slide-security-placeholder p {
    padding-top: 7px;
    color: #999;
    margin-right: -7px
}
 
.sign .overseas-btn {
    font-size: 14px;
    color: #999
}
 
.sign .overseas-btn:hover {
    color: #2f2f2f
}
 
.sign .remember-btn {
    float: left;
    margin: 15px 0
}
 
.sign .remember-btn span {
    margin-left: 5px;
    font-size: 15px;
    color: #969696;
    vertical-align: middle
}
 
.sign .forget-btn {
    float: right;
    position: relative;
    margin: 15px 0;
    font-size: 14px
}
 
.sign .forget-btn a {
    color: #999
}
 
.sign .forget-btn a:hover {
    color: #333
}
 
.sign .forget-btn .dropdown-menu {
    top: 20px;
    left: auto;
    right: 0;
    border-radius: 4px
}
 
.sign .forget-btn .dropdown-menu a {
    padding: 10px 20px;
    color: #333
}
 
.sign #sign-in-loading {
    position: relative;
    width: 20px;
    height: 20px;
    vertical-align: middle;
    margin-top: -4px;
    margin-right: 2px;
    display: none
}
 
.sign #sign-in-loading:after {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: transparent
}
 
.sign #sign-in-loading:before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border-radius: 10px;
    border: 2px solid #fff;
    border-bottom-color: transparent;
    vertical-align: middle;
    -webkit-animation: rolling .8s infinite linear;
    animation: rolling .8s infinite linear;
    z-index: 1
}
 
.sign .sign-in-button,.sign .sign-up-button {
    margin-top: 20px;
    width: 100%;
    padding: 9px 18px;
    font-size: 18px;
    border: none;
    border-radius: 25px;
    color: #fff;
    background: #42c02e;
    cursor: pointer;
    outline: none;
    display: block;
    clear: both
}
 
.sign .sign-in-button:hover,.sign .sign-up-button:hover {
    background: #3db922
}
 
.sign .sign-in-button {
    background: #3194d0
}
 
.sign .sign-in-button:hover {
    background: #187cb7
}
 
.sign .btn-in-resend,.sign .btn-up-resend {
    position: absolute;
    top: 7px;
    right: 7px;
    width: 100px;
    height: 36px;
    font-size: 13px;
    color: #fff;
    background-color: #42c02e;
    border-radius: 20px;
    line-height: 36px
}
 
.sign .btn-in-resend {
    background-color: #3194d0
}
 
.sign .sign-up-msg {
    margin: 10px 0;
    padding: 0;
    text-align: center;
    font-size: 12px;
    line-height: 20px;
    color: #969696
}
 
.sign .sign-up-msg a,.sign .sign-up-msg a:hover {
    color: #3194d0
}
 
.sign .overseas input {
    padding-left: 110px!important
}
 
.sign .overseas .overseas-number {
    position: absolute;
    top: 0;
    left: 0;
    width: 100px;
    height: 50px;
    font-size: 18px;
    color: #969696;
    border-right: 1px solid #c8c8c8
}
 
.sign .overseas .overseas-number span {
    margin-top: 17px;
    padding-left: 35px;
    text-align: left;
    font-size: 14px;
    display: block
}
 
.sign .overseas .dropdown-menu {
    width: 100%;
    max-height: 285px;
    font-size: 14px;
    border-radius: 0 0 4px 4px;
    overflow-y: auto
}
 
.sign .overseas .dropdown-menu li .nation-code {
    width: 65px;
    display: inline-block
}
 
.sign .overseas .dropdown-menu li a {
    padding: 6px 20px;
    font-size: 14px;
    line-height: 20px
}
 
.sign .overseas .dropdown-menu li a::hover {
    color: #fff;
    background-color: #f5f5f5
}
 
.sign .more-sign {
    margin-top: 50px
}
 
.sign .more-sign h6 {
    position: relative;
    margin: 0 0 10px;
    font-size: 12px;
    color: #b5b5b5
}
 
.sign .more-sign h6:before {
    left: 30px
}
 
.sign .more-sign h6:after,.sign .more-sign h6:before {
    content: "";
    border-top: 1px solid #b5b5b5;
    display: block;
    position: absolute;
    width: 60px;
    top: 5px
}
 
.sign .more-sign h6:after {
    right: 30px
}
 
.sign .more-sign ul {
    margin-bottom: 10px;
    list-style: none
}
 
.sign .more-sign ul li {
    margin: 0 5px;
    display: inline-block
}
 
.sign .more-sign ul a {
    width: 50px;
    height: 50px;
    line-height: 50px;
    display: block
}
 
.sign .more-sign ul i {
    font-size: 28px
}
 
.sign .more-sign .ic-weibo {
    color: #e05244
}
 
.sign .more-sign .ic-wechat {
    color: #00bb29
}
 
.sign .more-sign .ic-qq_connect {
    color: #498ad5
}
 
.sign .more-sign .ic-douban {
    color: #00820f
}
 
.sign .more-sign .ic-more {
    color: #999
}
 
.sign .more-sign .weibo-loading {
    pointer-events: none;
    cursor: pointer;
    position: relative
}
 
.sign .more-sign .weibo-loading:after {
    content: "";
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: #fff
}
 
body.reader-night-mode .sign .more-sign .weibo-loading:after {
    background-color: #3f3f3f
}
 
.sign .more-sign .weibo-loading:before {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 20px;
    height: 20px;
    margin: -10px 0 0 -10px;
    border-radius: 10px;
    border: 2px solid #e05244;
    border-bottom-color: transparent;
    vertical-align: middle;
    -webkit-animation: rolling .8s infinite linear;
    animation: rolling .8s infinite linear;
    z-index: 1
}
 
@keyframes rolling {
    0% {
        -webkit-transform: rotate(0deg);
        transform: rotate(0deg)
    }
 
    to {
        -webkit-transform: rotate(1turn);
        transform: rotate(1turn)
    }
}
 
@-webkit-keyframes rolling {
    0% {
        -webkit-transform: rotate(0deg)
    }
 
    to {
        -webkit-transform: rotate(1turn)
    }
}
 
.sign .reset-password-input {
    border-radius: 4px!important
}
 
.sign .return {
    margin-left: -8px;
    color: #969696
}
 
.sign .return:hover {
    color: #333
}
 
.sign .return i {
    margin-right: 5px
}
 
.sign .icheckbox_square-green {
    display: inline-block;
    *display: inline;
    vertical-align: middle;
    margin: 0;
    padding: 0;
    width: 18px;
    height: 18px;
    background: url(/static/image/green.png) no-repeat;
    border: none;
    cursor: pointer;
    background-position: 0 0
}
 
.sign .icheckbox_square-green.hover {
    background-position: -20px 0
}
 
.sign .icheckbox_square-green.checked {
    background-position: -40px 0
}
 
.sign .icheckbox_square-green.disabled {
    background-position: -60px 0;
    cursor: default
}
 
.sign .icheckbox_square-green.checked.disabled {
    background-position: -80px 0
}
 
 
.geetest_panel_box>* {
    box-sizing: content-box
}
 
@media (max-width:768px) {
    body {
        min-width: 0
    }
 
    .sign {
        height: auto;
        min-height: 0;
        background-color: transparent
    }
 
    .sign .logo {
        display: none
    }
 
    .sign .main {
        position: absolute;
        left: 50%;
        margin: 0 0 0 -200px;
        box-shadow: none
    }
}
</style>

2.设置路由

router/index.js

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import vue from 'vue'
import router from 'vue-router'
import home from '@/components/home'
import login from '@/components/login'
import register from "../src/components/register";
import qqloginindex from '@/components/oologinindex'
 
 
vue.vue(router)
 
export default new router({
  mode:'history',
  routes:[
    {
      path:'/',
      component:home
    },
    {
      path:'/login',
      component: login
    },
    {
      path:'/register',
      component: register
    },
    {
      path:'/oauth_callback.html',
      component: qqloginindex
    }
  ]
})

3.登录标签设置

header.vue

将a 标签设置为 router-link(站外链接调整为站内链接)

?
1
<router-link class="btn log-in" id="sign_in" to="/login">登录</router-link>

后端登录

2.1 创建一个用户表

虚拟环境下

?
1
2
cd renranapi/renranapi/apps
python ../../manage.py startapp users

2.2 settings/dev/py 新增配置

?
1
2
3
4
5
6
installed_apps = [
  
    #以下为第三方软件
    'corsheaders',
    'users'
]

2.3 运行时报错修& 改配置环境路径

报错

Django实现前后端登录

修改配置环境路径

settings.dev.py

?
1
2
修改配置环境路径
settings.dev.py

2.4 创建用户模型类

renranapi/apps/users/migrations/models.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.db import models
from django.contrib.auth.models import abstractuser
# create your models here.
class user(abstractuser):
    """用户模型类"""
    mobile = models.charfield(max_length=15, null=true,blank=true, unique=true, help_text="手机号码",verbose_name="手机号码")
    wxchat = models.charfield(max_length=100, null=true, blank=true, unique=true, help_text="微信账号",verbose_name="微信账号")
    alipay = models.charfield(max_length=100, null=true, blank=true, unique=true, help_text="支付宝账号",verbose_name="支付宝账号")
    qq_number = models.charfield(max_length=11, null=true, blank=true, unique=true, help_text="qq号",verbose_name="qq号")
    # 保存文件的子目录
    avatar = models.imagefield(upload_to="avatar", null=true,blank=true, default=none, verbose_name="头像")
 
    class meta:
        db_table = "rr_users"
        verbose_name = "用户信息"
        verbose_name_plural = verbose_name
 
    def __str__(self):
        return self.username

2.5 配置

settings/dev.py

?
1
auth_user_model = 'users.user'

2.6 数据库迁移

?
1
2
python ../../manage.py makemigrations
python ../../ manage.py migrate

设置 mysql

Django实现前后端登录

admin 配置

创建超级管理员

Django实现前后端登录

此时后台数据库

Django实现前后端登录

进入http://127.0.0.1:8000/admin/
root 用户登录:root 密码 123

以上就是django实现前后端登录的详细内容,更多关于django 前后端登录的资料请关注服务器之家其它相关文章!

原文链接:https://juejin.cn/post/6955010717447618567

延伸 · 阅读

精彩推荐