django-middleware

Is there a way to alter the request.path before matching the urls?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 02:27:18
问题 When I get a request for a path that includes the word 'self' I want to replace it with the user id before matching it to a URL. I tried using a middleware like this: def process_request(self, request): if '/self/' in request.path: request.path = request.path.replace('/self/','/' + str(request.user.id) + '/') The replacement works but apparently is done after the URL matching. Is there any way to alter the path before this point? 回答1: Apparently, the URL marching is not done using request

Is there a way to alter the request.path before matching the urls?

好久不见. 提交于 2019-12-19 02:27:07
问题 When I get a request for a path that includes the word 'self' I want to replace it with the user id before matching it to a URL. I tried using a middleware like this: def process_request(self, request): if '/self/' in request.path: request.path = request.path.replace('/self/','/' + str(request.user.id) + '/') The replacement works but apparently is done after the URL matching. Is there any way to alter the path before this point? 回答1: Apparently, the URL marching is not done using request

Django - Runtime database switching

人盡茶涼 提交于 2019-12-18 21:19:44
问题 In my work we want to run a server with multiple databases. The databases switching should occur when you acces a url like http://myapp.webpage.com or http://other.webpage.com . We want to run only one server instance and at the moment of the HTTP request switch the database and return the corresponding response. We've been looking for a mantainable and 'Django-friendly' solution. In our investigation we have found possible ways to do this, but we have not enough information about. Option 1:

Django: Overwrite ROOT_URLCONF with request.urlconf in middleware

青春壹個敷衍的年華 提交于 2019-12-18 13:35:29
问题 I am trying to overwrite ROOT_URLCONF with another url when the request contains "api" subdomain and this is what I have so far. from django.utils.cache import patch_vary_headers class SubdomainMiddleware: def process_request(self, request): path = request.get_full_path() root_url = path.split('/')[1] domain_parts = request.get_host().split('.') if (len(domain_parts) > 2): subdomain = domain_parts[0] if (subdomain.lower() == 'www'): subdomain = None else: subdomain = None request.subdomain =

Django exception middleware: TypeError: object() takes no parameters

和自甴很熟 提交于 2019-12-18 11:15:41
问题 I'm using Django 1.10 and trying to catch all exceptions with exception middleware. The code below causes an internal server error: mw_instance = middleware(handler) TypeError: object() takes no parameters views.py from django.http import HttpResponse def my_view(request): x = 1/0 # cause an exception return HttpResponse("ok") settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common

Django: “No module named context_processors” error after reboot

非 Y 不嫁゛ 提交于 2019-12-18 03:49:32
问题 I have a Django site that works on my PC, and was working briefly on my server after loading it on. I noticed my server had Django 1.6 and I upgraded to 1.8. After rebooting, none of the pages on my site load and I get the error: ImportError No module named context_processors I read through the docs on Django and allauth. Django mentions that in 1.8 the context_processors moved and allauth says specific allauth tags are no longer needed in the TEMPLATE_CONTEXT_PROCESSORS of settings.py .

Modify address in Django middleware

…衆ロ難τιáo~ 提交于 2019-12-13 14:30:04
问题 I don't know if it's possible but I'd like to add few parameters at the end of the URL using middleware. Can it be done without redirect after modyfing requested URL? ie. user clicks: .../some_link and middleware rewrites it to: .../some_link?par1=1&par2=2 Other way is to modify reponse and replace every HTML link but it's not something I'd like to do. Thanks 回答1: I think this really depends on your problem and what exactly you are trying to do. You cannot change the URL without redirecting

What is a django middleware equivalent in ASP MVC?

天大地大妈咪最大 提交于 2019-12-12 01:37:11
问题 Basically, I want to inject some data into ViewData/ViewBag for every single request. 回答1: In ASP.NET MVC that would be an action filter. And if you want to do it globally you could register it as a global action filter. This way it will apply to all controller actions so that you don't need to decorate them individually. So your filter could be defined like this: public class GlobalViewBagInjectorActionFilter : ActionFilterAttribute { public override void OnActionExecuted

Can't view django 2.0 admin page after upgrading

拟墨画扇 提交于 2019-12-11 16:05:18
问题 I've just been porting to Python3 from 2 and upgrading Django from 1.7 to 2.0 (massive changes I know). I'm using Heroku to host the app. When I run heroku local or just run the app locally with manage.py runserver the app loads but navigating to the /admin page comes up with the error: Page not found (404) Request Method: GET Request URL: http://127.0.0.1:5000/admin Using the URLconf defined in loowatt.urls, Django tried these URL patterns, in this order: write/ admin/ ^$ [name='index'] The

Django allow only one user session in total

蹲街弑〆低调 提交于 2019-12-11 14:23:32
问题 I currently try to implement a policy to my application that only one user session at a time is allowed, if a user trys to log-in from another device the old sessions gets killed. But for some resone i get the following error and i can't find the mistake myself :( : RelatedObjectDoesNotExist at / User has no logged_in_user. My project contains two apps, the actually app and a "accounts" app that contains all informations shown here. signals.py # Signals that fires when a user logs in and logs