注册作业

url

from django.conf.urls import url
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^login/', views.login),
    url(r'^register/', views.register),
]

  

views

from django.shortcuts import render,HttpResponse,redirect
from app01 import models


# Create your views here.
def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user_obj = models.User.objects.filter(username=username).first()
        if user_obj:
            if password == user_obj.password:
                return HttpResponse("登陆成功")
            else:
                return HttpResponse("密码错误")
        else:
            return HttpResponse("用户不存在")
    return render(request, 'login.html')

def register(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user_obj = models.User.objects.filter(username=username).first()
        if not user_obj:
            user_obj = models.User(username=username, password=password)
            user_obj.save()
            return HttpResponse("注册成功")
        else:
            return HttpResponse("用户已存在")
    return render(request, 'register.html')

  

model

from django.db import models

# Create your models here.
class User(models.Model):
    id = models.AutoField(primary_key=True,verbose_name='主键')
    username = models.CharField(max_length=32,verbose_name='用户名')
    password = models.CharField(max_length=9,verbose_name="密码")

    def __str__(self):
        return '%s'%self.username

  

posted @ 2020-05-25 22:30  清轩挽长风  阅读(119)  评论(0编辑  收藏  举报