sesson和cookie的使用

 

 

book_manager 的views.py

from django.shortcuts import render, redirect, reverse, HttpResponse
from app01 import models
import time
from django.conf import global_settings

def login_required(func):
    def inner(request, *args, **kwargs):
        # is_login = request.COOKIES.get('is_login')
        # is_login = request.get_signed_cookie('is_login', salt='xxxx', default='')
        is_login = request.session.get('is_login')
        print(is_login)
        if is_login != '1':
            url = request.path_info
            
            return redirect('/login/?next={}'.format(url))
        ret = func(request, *args, **kwargs)
        
        return ret
    
    return inner


def login(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        pwd = request.POST.get('pwd')
        if username == 'alex' and pwd == '123':
            
            url = request.GET.get('next')
            if url:
                ret = redirect(url)
            else:
                ret = redirect(reverse('index'))
            
            # ret.set_cookie('is_login', '1', path='/app01/publisher_list/')  # 普通cookie
            # ret.set_signed_cookie('is_login', '1', salt='xxxx')  # 普通cookie
            request.session['is_login'] = '1'
            request.session.set_expiry(0)
            return ret
    return render(request, 'login.html')


def logout(request):
    ret = redirect('/login/')
    # ret.delete_cookie('is_login')
    
    # 删除session
    request.session.delete()
    
    return ret


@login_required
def index(request):
    return HttpResponse('o98k')


def timer(func):
    def inner(request, *args, **kwargs):
        print(func)
        print(args)
        start = time.time()
        ret = func(request, *args, **kwargs)
        print("执行的时间:{}".format(time.time() - start))
        return ret
    
    return inner


@login_required
def publisher(reqeust):
    # 测试request对象
    # print(reqeust.path_info)
    # print(reqeust.get_full_path())
    
    # 测试session
    print(reqeust.session.session_key)
    
    
    # print(reqeust.scheme)
    # print(reqeust.META)
    
    # url = reverse('edit_pub', args=('publisher','100',))
    # url = reverse('edit_pub', kwargs={'name': 'publisher', 'pk': '100'})
    # url = reverse('publisher')
    #
    # print(url, type(url))
    # 获取到所有的出版社信息
    all_publisher = models.Publisher.objects.all()
    
    return render(reqeust, 'publisher.html', {'publishers': all_publisher, 'name': 'base.html'})


@timer
def add_publisher(request):
    if request.method == 'POST':
        new_name = request.POST.get('new_name')
        obj = models.Publisher.objects.create(name=new_name)
        print(obj, type(obj))
        return redirect(reverse('publisher'))
    
    return render(request, 'add_publisher.html')


from django.views import View
from django.utils.decorators import method_decorator


# @method_decorator(timer, name='get')
# @method_decorator(timer, name='post')
@method_decorator(login_required, name='dispatch')
class AddPub(View):
    # http_method_names = ['get']
    
    # @timer
    def dispatch(self, request, *args, **kwargs):
        ret = super().dispatch(request, *args, **kwargs)
        return ret
    
    # @timer
    def get(self, request):
        print(request.body)
        print('get')
        return render(request, 'add_publisher.html')
    
    # @method_decorator(timer)
    def post(self, request):
        print(request.body)
        print('post')
        new_name = request.POST.get('new_name')
        obj = models.Publisher.objects.create(name=new_name)
        return redirect(reverse('publisher'))


def edit_publisher(request, name, pk):
    # pk = request.GET.get('pk')
    obj = models.Publisher.objects.filter(pk=pk).first()
    
    if request.method == 'POST':
        new_name = request.POST.get('new_name')
        obj.name = new_name
        obj.save()
        return redirect(reverse('publisher'))
    
    return render(request, 'edit_publisher.html', {'obj': obj})


def book_list(request):
    books = models.Book.objects.all()
    
    # for book in books:
    #     print(book.title)
    #     print(book.publisher, type(book.publisher))   # 对象.外键  _ > 所挂关联的对象
    #     print(book.publisher.pid)
    #     print(book.publisher.name)
    #     print(book.publisher_id)
    
    return render(request, 'book_list.html', {'books': books})


class AddBook(View):
    
    def get(self, request):
        publishers = models.Publisher.objects.all()
        return render(request, 'add_book.html', {'publishers': publishers})
    
    def post(self, request):
        new_name = request.POST.get('new_name')
        pub_id = request.POST.get('pub')
        # models.Book.objects.create(title=new_name,publisher=models.Publisher.objects.get(pk=pub_id))
        models.Book.objects.create(title=new_name, publisher_id=pub_id)
        return redirect(reverse('book'))


class EditBook(View):
    
    def get(self, request, pk):
        book_obj = models.Book.objects.get(pk=pk)
        
        publishers = models.Publisher.objects.all()
        return render(request, 'edit_book.html', {'book_obj': book_obj, 'publishers': publishers})
    
    def post(self, request, pk):
        book_obj = models.Book.objects.get(pk=pk)
        
        new_name = request.POST.get('new_name')
        pub_id = request.POST.get('pub')
        book_obj.title = new_name
        book_obj.publisher_id = pub_id
        book_obj.save()
        
        return redirect(reverse('book'))


def delete(request, table, pk):
    table_class = getattr(models, table.capitalize())
    table_class.objects.filter(pk=pk).delete()
    return redirect(reverse(table))

 

posted @ 2019-03-08 15:32  小菜鸟111  阅读(301)  评论(0编辑  收藏  举报