django-middleware

Change Django Templates Based on User-Agent

丶灬走出姿态 提交于 2019-11-28 03:08:45
I've made a Django site, but I've drank the Koolaid and I want to make an IPhone version. After putting much thought into I've come up with two options: Make a whole other site, like i.xxxx.com. Tie it into the same database using Django's sites framework. Find some time of middleware that reads the user-agent, and changes the template directories dynamically. I'd really prefer option #2, however; I have some reservations, mainly because the Django documentation discourages changing settings on the fly . I found a snippet that would do the what I'd like. My main issue is having it as seamless

Django: WSGIRequest' object has no attribute 'user' on some pages?

纵饮孤独 提交于 2019-11-28 00:42:07
I want to set a cookie if user is logged in or not. My middleware: class UserStatus(object): def process_response(self,request,response): user_status = 1 if request.user.is_authenticated() else 0 max_age = (20)*52*7*24*60*60 # 20 years (After expiry, cookie gets deleted) response.set_cookie(user_status_cookie,user_status,max_age) return response Added to MIDDLEWARE_CLASSES in settings.py at the end. Problem: Error: 'WSGIRequest' object has no attribute 'user' Why, when I have the Authentication and the Session middlewares active already ? Also, some pages are working smooth where as some are

Forbidden (403) CSRF verification failed. Request aborted

非 Y 不嫁゛ 提交于 2019-11-27 16:33:54
问题 I am making an app of login form but when I am running my app and click on login button the following error will occur Forbidden (403) CSRF verification failed. Request aborted. the code of view.py is as: from django.template import loader from django.shortcuts import render_to_response from registration.models import Registration from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import redirect def view_login(request,registration_id): t =

Execute code in Django after response has been sent to the client

随声附和 提交于 2019-11-27 13:15:31
In my Django application I want to keep track of whether a response has been sent to the client successfully. I am well aware that there is no "watertight" way in a connectionless protocol like HTTP to ensure the client has received (and displayed) a response, so this will not be mission-critical functionality, but still I want to do this at the latest possible time. The response will not be HTML so any callbacks from the client (using Javascript or IMG tags etc.) are not possible. The "latest" hook I can find would be adding a custom middleware implementing process_response at the first

How to set up custom middleware in Django

浪尽此生 提交于 2019-11-27 11:11:02
I am trying to create middleware to optionally pass a kwarg to every view that meets a condition. The problem is that I cannot find an example of how to set up the middleware. I have seen classes that override the method I want to, process_view : Class CheckConditionMiddleware(object): def process_view(self, request): return None But where do I put this class? Do I create a middleware app and put this class inside of it and then reference it in settings.middleware ? First: The path structure If you don't have it you need to create the middleware folder within your app following the structure:

Non-global middleware in Django

这一生的挚爱 提交于 2019-11-27 10:30:19
问题 In Django there is a settings file that defines the middleware to be run on each request. This middleware setting is global. Is there a way to specify a set of middleware on a per-view basis? I want to have specific urls use a set of middleware different from the global set. 回答1: You want decorator_from_middleware. from django.utils.decorators import decorator_from_middleware @decorator_from_middleware(MyMiddleware) def view_function(request): #blah blah It doesn't apply to URLs, but it works

Change Django Templates Based on User-Agent

随声附和 提交于 2019-11-26 23:56:50
问题 I've made a Django site, but I've drank the Koolaid and I want to make an IPhone version. After putting much thought into I've come up with two options: Make a whole other site, like i.xxxx.com. Tie it into the same database using Django's sites framework. Find some time of middleware that reads the user-agent, and changes the template directories dynamically. I'd really prefer option #2, however; I have some reservations, mainly because the Django documentation discourages changing settings

Django: WSGIRequest' object has no attribute 'user' on some pages?

三世轮回 提交于 2019-11-26 21:48:34
问题 I want to set a cookie if user is logged in or not. My middleware: class UserStatus(object): def process_response(self,request,response): user_status = 1 if request.user.is_authenticated() else 0 max_age = (20)*52*7*24*60*60 # 20 years (After expiry, cookie gets deleted) response.set_cookie(user_status_cookie,user_status,max_age) return response Added to MIDDLEWARE_CLASSES in settings.py at the end. Problem: Error: 'WSGIRequest' object has no attribute 'user' Why, when I have the

Execute code in Django after response has been sent to the client

孤者浪人 提交于 2019-11-26 16:15:29
问题 In my Django application I want to keep track of whether a response has been sent to the client successfully. I am well aware that there is no "watertight" way in a connectionless protocol like HTTP to ensure the client has received (and displayed) a response, so this will not be mission-critical functionality, but still I want to do this at the latest possible time. The response will not be HTML so any callbacks from the client (using Javascript or IMG tags etc.) are not possible. The

How to set up custom middleware in Django

故事扮演 提交于 2019-11-26 12:02:22
问题 I am trying to create middleware to optionally pass a kwarg to every view that meets a condition. The problem is that I cannot find an example of how to set up the middleware. I have seen classes that override the method I want to, process_view : Class CheckConditionMiddleware(object): def process_view(self, request): return None But where do I put this class? Do I create a middleware app and put this class inside of it and then reference it in settings.middleware ? 回答1: First: The path