码迷,mamicode.com
首页 > 编程语言 > 详细

基于Python+Django重定向的例子

时间:2018-10-04 10:48:44      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:https   user   uri   red   edr   parse   nbsp   you   django   

Django源码, 这里HttpResponseRedirect和HttpResponsePermanentRedirect没有太大差别,前者是返回302临时重定向,后者返回301永久重定向

 1 class HttpResponseRedirectBase(HttpResponse):
 2     allowed_schemes = [http, https, ftp]
 3 
 4     def __init__(self, redirect_to, *args, **kwargs):
 5         super().__init__(*args, **kwargs)
 6         self[Location] = iri_to_uri(redirect_to)
 7         parsed = urlparse(str(redirect_to))
 8         if parsed.scheme and parsed.scheme not in self.allowed_schemes:
 9             raise DisallowedRedirect("Unsafe redirect to URL with protocol ‘%s‘" % parsed.scheme)
10 
11     url = property(lambda self: self[Location])
12 
13     def __repr__(self):
14         return <%(cls)s status_code=%(status_code)d%(content_type)s, url="%(url)s"> % {
15             cls: self.__class__.__name__,
16             status_code: self.status_code,
17             content_type: self._content_type_for_repr,
18             url: self.url,
19         }
20 
21 
22 class HttpResponseRedirect(HttpResponseRedirectBase):
23     status_code = 302
24 
25 
26 class HttpResponsePermanentRedirect(HttpResponseRedirectBase):
27     status_code = 301
28 
29 
30 class HttpResponseNotModified(HttpResponse):
31     status_code = 304
32 
33     def __init__(self, *args, **kwargs):
34         super().__init__(*args, **kwargs)
35         del self[content-type]
36 
37     @HttpResponse.content.setter
38     def content(self, value):
39         if value:
40             raise AttributeError("You cannot set content to a 304 (Not Modified) response")
41         self._container = []

小栗子:

1 def redirectdemo(request):
2     dic_params = {username: request.GET.get(username),
3                   password: request.GET.get(password)}
4     if validate_parameters(dic_params):
5         return HttpResponsePermanentRedirect("http://www.baidu.com")
6     else:
7         return HttpResponse(<html>error page</html>)

 

基于Python+Django重定向的例子

标签:https   user   uri   red   edr   parse   nbsp   you   django   

原文地址:https://www.cnblogs.com/andrew209/p/9740044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!