Django框架之views(業務邏輯)簡單後臺註冊業務邏輯詳細描述


from django.shortcuts import render,HttpResponse,HttpResponseRedirect
import models
from django.db.models import Q  # 模糊查詢使用的對象
from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger    # 分頁使用的類和異常處理的包
from django.contrib import auth      # 登錄,退出,驗證使用的包
from django.contrib.auth.decorators import login_required       # 防止未登錄就直接訪問後臺頁面使用的包
from django.contrib.auth.models import User        # 引入用戶表
from django.contrib.auth.hashers import make_password    # 密碼加密使用的包
import os
import uuid   # 生成唯一標識符
# 註冊頁面
def reg(request):
    if request.method == 'POST':
        # 收集用戶信息
        username = request.POST.get('username',None)
        password1 = request.POST.get('password1',None)
        password2 = request.POST.get('password2',None)
        nick = request.POST.get('nick',"")
        phone = request.POST.get('phone',"")
        print username
        if username and password1 and password2:
            if password1 == password2:
                # 判斷用戶名在用戶表中是否存在
                u_count = User.objects.filter(username=username).count()    # 查看結果記錄條數
                if u_count == 0:   # 沒有這個用戶名
                    # 添加自帶用戶表
                    user_info = {
                        'username':username,
                        'password':make_password(password2)
                    }
                    user_info = User.objects.create(**user_info)
                    # 添加擴展表的擴展信息
                    user_profile={
                        'nick':nick,
                        'phone':phone,
                        'user':user_info
                    }
                    models.UserProfile.objects.create(**user_profile)
                    return HttpResponseRedirect('/pro01/login/')
                else:
                    return render(request, 'pro01/reg.html', {'error': '用戶名已存在'})
            else:
                return render(request,'pro01/reg.html',{'error':'兩次密碼不相同'})
    else:
        return render(request,'pro01/reg.html')
# ajax驗證用戶名是否存在

def checkname(request):
    # 獲取ajax傳遞的name
    name = request.GET.get('name',None)
    res = models.User.objects.filter(username=name).count()   # 受影響行數
    if res == 0:
        return HttpResponse('ok')
    else:
        return HttpResponse('用戶名已經存在')

注意:以上內容是個人使用的隨手記錄, 就是介紹了下簡單的使用

歡迎大家來吐槽,準備好瓜子飲料礦泉水,開整!!!

---------------------------------------------------------------------------------------

搞笑:能動手就儘量別吵吵


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章