python django學習一:簡單註冊/登陸/session

來源:互聯網
上載者:User

標籤:username   bin   go import   name   doc   field   user   使用   head   

註冊 登陸 sessionuser.html

未使用{{useform}}而使用{{ userform.password }}形式便於後期css樣式

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <title>Title</title></head><body><form action="." method="POST">    {% csrf_token %}    <div>username: <input type="text" name="username"> </div>    <div>password: {{ userform.password }}</div>    <div><input type="submit" value="註冊" name="reg"> <input type="submit" value="登陸" name="login"> </div></form></body></html>
form.py
#/usr/bin/env python3from django import  formsclass UserForm(forms.Form):    username=forms.CharField()    password=forms.CharField()
models.py
from django.db import  modelsclass User(models.Model):    username=models.CharField(max_length=30)    password=models.CharField(max_length=30)    def __str__(self):        return self.username
viesw.py
from django.shortcuts import renderfrom django.views.decorators.cache import  cache_pagefrom .models import  User# Create your views here.from django.http import  HttpResponsefrom django.http import  HttpResponseRedirectfrom djapp.forms import UserFormdef user(request):    if request.method == ‘POST‘:        userform=UserForm(request.POST)        if userform.is_valid():            if request.POST.get(‘reg‘):                username=userform.cleaned_data[‘username‘]                password=userform.cleaned_data[‘password‘]                User.objects.create(username=username,password=password)                # user=User()                # user.username=username                # user.password=password                # user.save()                return  HttpResponse("login 註冊%s"%username)            else:                if request.session.get(‘user_id‘) == 3:                    return  HttpResponse("session 登陸")                else:                    try:                        user=User.objects.get(username=request.POST.get(‘username‘,None))                    except Exception as e:                        print(e)                        return HttpResponse("找不到使用者")                    if user.password == request.POST.get(‘password‘):                        request.session[‘user_id‘]=user.id                        return HttpResponse("登陸成功")                    else:                        return  HttpResponse("登陸失敗")    else:        userform=UserForm()        return render(request,‘djapp/user.html‘,{‘userform‘:userform})
網頁

cookie儲存sessionid

django_session表內容

python django學習一:簡單註冊/登陸/session

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.