django-middleware

Django - counting model instance views (for a “top entries” app)

和自甴很熟 提交于 2019-12-01 13:59:56
I'm new, and confused. I want to create a module that keeps track of the "top hit" instances of both an article and a blog model. I don't want to touch the code for the article or blog models. Is this a candidate for middleware? looking at the HttpRequest.path ? Middleware looking at request.path is ugly, as it introduces a dependency on the details of the URL patterns you use to display articles and blog posts. If you don't mind this coupling, then you might as well just save the performance hit and do your analysis on webserver log files. ( EDIT : view middleware would be a better option, as

'WSGIRequest' object has no attribute 'session'

*爱你&永不变心* 提交于 2019-11-30 22:26:40
问题 I get this error sometimes in custom Middleware in process_response method. I have the following list of middlewares: MIDDLEWARE_CLASSES = [ 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.middleware.doc.XViewMiddleware', 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.csrf

Django - Runtime database switching

笑着哭i 提交于 2019-11-30 19:42:46
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 middleware The django middleware runs each time the server receive a HTTP request. Making a

Django: Overwrite ROOT_URLCONF with request.urlconf in middleware

痞子三分冷 提交于 2019-11-30 10:06:31
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 = subdomain request.domain = domain if request.subdomain == "api": request.urlconf = "rest_api_example

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

…衆ロ難τιáo~ 提交于 2019-11-30 04:11:36
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.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware

Django: “No module named context_processors” error after reboot

南笙酒味 提交于 2019-11-29 02:55:08
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 . Django: https://docs.djangoproject.com/en/1.8/ref/settings/ Allauth: https://django-allauth.readthedocs.org

Forbidden (403) CSRF verification failed. Request aborted

本小妞迷上赌 提交于 2019-11-29 02:09:01
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 = loader.get_template('registration/login.html') try: registration=Registration.objects.get(pk

Python and Django OperationalError (2006, 'MySQL server has gone away')

雨燕双飞 提交于 2019-11-28 22:49:30
Original: I have recently started getting MySQL OperationalErrors from some of my old code and cannot seem to trace back the problem. Since it was working before, I thought it may have been a software update that broke something. I am using python 2.7 with django runfcgi with nginx. Here is my original code: views.py DBNAME = "test" DBIP = "localhost" DBUSER = "django" DBPASS = "password" db = MySQLdb.connect(DBIP,DBUSER,DBPASS,DBNAME) cursor = db.cursor() def list(request): statement = "SELECT item from table where selected = 1" cursor.execute(statement) results = cursor.fetchall() I have

Non-global middleware in Django

三世轮回 提交于 2019-11-28 17:28:04
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. Ned Batchelder 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 per-view, so you can have fine-grained control over its effect. I have a real solution for

Practical rules for Django MiddleWare ordering?

≡放荡痞女 提交于 2019-11-28 15:35:57
The official documentation is a bit messy: 'before' & 'after' are used for ordering MiddleWare in a tuple, but in some places 'before'&'after' refers to request-response phases. Also, 'should be first/last' are mixed and it's not clear which one to use as 'first'. I do understand the difference.. however it seems to complicated for a newbie in Django. Can you suggest some correct ordering for builtin MiddleWare classes (assuming we enable all of them) and — most importantly — explain WHY one goes before/after other ones? here's the list, with the info from docs I managed to find: