Python uses the decorator for django Development Instance code, pythondjango

Source: Internet
Author: User

Python uses the decorator for django Development Instance code, pythondjango

This article focuses on the development of django using the decorator in Python. The details are as follows.

The decorator Can process a function, method, or class and add additional functions.

In this article, add sessions to the page using the decorator instead of directly accessing index, and show. In views. py

def index(request):    return HttpResponse('index') def show(request):    return HttpResponse('show')

In this way, you can directly access index and show. If only logged-in users are allowed to access index and show, you need to modify the code.

def index(request):    if request.session.get('username'):      return HttpResponse('index')    else:      return HttpResponse('login')<br data-filtered="filtered">def show(request):    if request.session.get('username'):      return HttpResponse('show')    else:      return HttpResponse('login')

In this way, you can restrict the access of logged-on users, but many identical parts also appear in the code, so you can write these identical parts into a function, use such a function to describe index and show. Such a function is the decorator..

Def decorator (main_func): def wrapper (request): # index, show is a parameter, so wrapper is also a parameter if request. session. get ('username'): return main_func (request) else: return HttpResponse ('login') return wrapper @ decoratordef index (request): return HttpResponse ('index ') def show (request): return HttpResponse ('show ')

In this way, you can use the decorator function to decorate a view function as long as it is a parameter. If there are two parameters, You need to modify the decorator.

def decorator(main_func):  def wrapper(request):           if request.session.get('username'):      return main_func(request)    else:      return HttpResponse('login')  return wrapper def decorator1(main_func):  def wrapper(request,page):           if request.session.get('username'):      return main_func(request,page)    else:      return HttpResponse('login')  return wrapper @decoratordef index(request):  return HttpResponse('index') @decorator1def show(request,page):  return HttpResponse('show')

If there is a parameter, it will be modified through decorator. If there are two parameters, it will be modified through decorator1. Therefore, you can combine decorator and decorator1 through dynamic parameters to modify both index and show.

def decorator3(main_func):    def wrapper(request,*args,**kwargs):        if not request.session.get('username'):            return main_func(request,*args,**kwargs)        else:            return HttpResponse('login')    return wrapper  @decorator3def index(request,*args,**kwargs):    return HttpResponse('index')@decorator3def show(request,*args,**kwargs):    return HttpResponse('show')
Summary

The above is all the content about the django Development Instance code using the decorator in Python. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.