django-middleware

django middleware redirect infinite loop

六月ゝ 毕业季﹏ 提交于 2019-12-11 08:07:03
问题 I have a middleware that checks a session value and redirects depending that value. My problem is, it is creating an infinite redirect loop and I'm not sure why. So, what I want to do is check to see if the value of the session visible is yes and if not redirect the user to my test view. Here is my middleware: class CheckStatus(object): def process_request(self, request): if request.user.is_authenticated(): s = request.session.get('visible') if str(s) is not 'yes': return HttpResponseRedirect

django-The page isn’t redirecting properly

被刻印的时光 ゝ 提交于 2019-12-11 07:01:50
问题 I have a middleware that check user profile. If auth user doesn't have a profile, then redirect to user profile. My browser displays the error The page isn’t redirecting properly . class Check(MiddlewareMixin): def process_request(self, request): if request.user.is_authenticated(): user = request.user try: profile = Profile.objects.get(user_id = user) if profile: pass except ObjectDoesNotExist: return HttpResponseRedirect('/accounts/profile/') I'm use django-allauth . 回答1: It sounds like you

Error with django middleware

巧了我就是萌 提交于 2019-12-11 06:05:20
问题 I am trying to select database dynamically with help of a middleware(which sets a variable in threading.local) and a dbrouter(which sets the database depending upon the variable set in threading.local). The code goes on like this: import re import threading request_cfg = threading.local() class RouterMiddleware(object): def process_view( self, request, view_func, view_args, view_kwargs ): pattern = re.compile("\\b(http://|https://|www.|.com|8000|:|//)\\W\\d+", re.I) words = request.get_host()

Django 1.10: “new style” middleware equivalent of `process_request()`

折月煮酒 提交于 2019-12-10 15:25:58
问题 How would one create "new style" middleware, which fulfills an equivalent implementation to using the process_request() hook with the "old style"? I've already adapted pre 1.10 middleware process_request() using MiddlewareMixin ... from django.utils.deprecation import MiddlewareMixin class MyCustomMiddleware(MiddlewareMixin): def process_request(self, request): # My request logic return response I'd like to know how to do a "pure" >1.9 "new style" implementation. I tried doing so by

For a Django middleware class, how can process_request work just fine, but process_exception not be calls?

♀尐吖头ヾ 提交于 2019-12-10 15:06:39
问题 I've created my own middleware class in Django that was working just fine until recently. The strange thing is that process_request still gets called just fine, but even when the response is 500 - internal server error, process_exception is not called at all. Why? It makes no difference whether I declare my middleware class as the first or the last entry in the list of installed middleware in the settings file. Thanks, Dave 回答1: The process_exception only gets invoked when the view raises an

Django ImportError: No module named middleware

核能气质少年 提交于 2019-12-10 03:52:26
问题 I am using Django version 1.8 and python 2.7. I am getting the following error after running my project. Traceback (most recent call last): File "C:\Python27\lib\wsgiref\handlers.py", line 85, in run self.result = application(self.environ, self.start_response) File "C:\Python27\lib\site-packages\django\contrib\staticfiles\handlers.py", line 63, in __call__ return self.application(environ, start_response) File "C:\Python27\lib\site-packages\django\core\handlers\wsgi.py", line 170, in __call__

Mobile templates based on user-agent in django ensuring thread safety

拜拜、爱过 提交于 2019-12-08 05:27:10
问题 I am developing the mobile version of my website, so thought of using user-agent as the criteria for serving different templates for mobile and web version. I successfully read the user-agent information from nginx and passed it as header to gunicorn server. Then I created a middleware which reads this header and changes the templates directory in settings file. This seemed to work initially but then I realized that there is race condition happening as this method is not thread safe. (I

Browser delay with changing content of the page in django admin (caching, python/django)

吃可爱长大的小学妹 提交于 2019-12-07 22:58:14
问题 I have a bit weird problem witch caching on my project in django. I can edit my page-content in django-admin. When i do that and refresh site - nothing is happening. I have to wait few minutes for changes. Funny thing is that, when i change browser (or computer) - i dont have to wait - the changes are on. Is it the problem of django, browser or what? Is it possible to set setting.py to get changes immediately? By the way, i have already figured out that when i turn the "django.middleware

Django: Remove duplicate messages from storage

时光总嘲笑我的痴心妄想 提交于 2019-12-07 19:34:42
问题 I'm using messages to add flash messages to the template (just as you'd expect). The problem I have is that if you double click a link to a page that generates a message then the message appears twice. I am using the message to tell the user I have redirected them from where they were expecting to go. They don;t need the same message twice. I understand the logic here but am wondering how I can remove duplicated messages. click url message generated, saved in storage click url again before

Editing response content in Django middleware

天涯浪子 提交于 2019-12-07 15:22:44
问题 I have Django 1.10 project and the following user-defined middleware class RequestLogMiddleWare(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): response = self.get_response(request) response.data['detail'] = 'I have been edited' return response and a REST-endpoint view: def r_mobile_call_log(request): return Response({'success': True, 'detail': 'Before having been edited'}, status=status.HTTP_200_OK) So I would expect the final response