django-middleware

middleware updates request.user, but request.user becomes AnonymousUser in view

元气小坏坏 提交于 2021-02-17 06:38:48
问题 I wrote a simple JWT middleware to get user from the JWT. The method get_user_from_jwt returns a User object. # app.middlewares.py class JwtMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): self.process_request(request) return self.get_response(request) def process_request(self, request): request.user = self.get_user_from_jwt(request.headers) def get_user_pk_from_jwt(self, auth_header: str) -> int: _, token = auth_header.split(' ')

Django rest framework custom Response middleware

强颜欢笑 提交于 2021-01-29 03:53:25
问题 I use Django Rest Framework with rest_auth (/login, /logout/, /register...) I have a Middleware, normally triggered by user_logged_in or user_logged_out signals. In my Middleware, I want to use Response object from Rest framework. My middleware.py from django.contrib.sessions.models import Session from django.contrib.auth import logout from django.contrib.auth.models import AnonymousUser from rest_framework.response import Response from rest_framework import status class

Django JWT authentication - user is anonymous in middleware

霸气de小男生 提交于 2021-01-27 16:24:13
问题 I am using Django JWT to power up authentication system in my project. Also, I have a middleware, and the problem is that inside it, the user is anonymous for some reason, while in the view I am able to access the correct user by request.user . This issue is driving me crazy because some time ago this code worked perfectly ! Is this JWT's bug or I am doing something wrong ? class TimezoneMiddleware(MiddlewareMixin): def process_request(self, request): # request.user is ANONYMOUS HERE !!!! if

django error: ImproperlyConfigured: WSGI application

强颜欢笑 提交于 2021-01-27 04:25:11
问题 My application was working last night, not sure why it won't work this morning. I think that all I did was to create an app called django to store my models, tests, and views. Getting this error, running django with the Heroku Postgres application on OS X and dj_database as middleware: File "/Users/{ME}/Projects/{PROJECT}/{PROJECT}/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 58, in get_internal_wsgi_application "could not import module '%s': %s" % (app_path, module_name

Access named URL parameter in Template or Middleware

痴心易碎 提交于 2020-07-29 12:15:17
问题 In my url conf, I have several URL's which have the same named parameter, user_id . Is it possible to access this parameter either in a middleware - so I can generically pass it on to the context_data - or in the template itself? Sample URL conf to illustrate the question: url(r'^b/(?P<user_id>[0-9]+)/edit?$', user.edit.EditUser.as_view(), name='user_edit'), url(r'^b/(?P<user_id>[0-9]+)/delete?$', user.delete.DeleteUser.as_view(), name='user_delete') 回答1: If you need this data in the template

'WSGIRequest' object has no attribute 'user'

≯℡__Kan透↙ 提交于 2020-05-11 20:43:38
问题 I'am trying to make an auth module in my django project. But when I open my web site url I have a this error: 'WSGIRequest' object has no attribute 'user' I've trying to find information about this problem and somebody said that the problem is in MIDDLEWARE_CLASSES but I can't understand it. This is my MIDDLEWARE_CLASSES: MIDDLEWARE_CLASSES = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware',

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

核能气质少年 提交于 2020-01-21 10:26:20
问题 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 ? 回答1: 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

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

不羁岁月 提交于 2020-01-21 10:26:09
问题 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 ? 回答1: 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

How to implement Https(SSL Middleware) in Django

不打扰是莪最后的温柔 提交于 2019-12-22 18:13:51
问题 I am a newbie in Django and web development. I want to implement Exactly this Question, but in django. I have searched many blogs and questions, nowhere was i able to find,exactly how to implement this. The SSL Middleware Django, is something i couldnt grasp very well. If that is the solution, can anyone please tell me how to implement it? Is the question clear ? or do i need to add a few things, please comment i will make the necessary changes.Any help will be highly appreciated.Thanks in

Django Error Reporting - How to know which user triggered the error?

笑着哭i 提交于 2019-12-21 09:23:29
问题 Is there a way I can customize Django error reporting so when it emails me it lets me know which user triggered the error? I'm in Django 1.2 if it matters. Much Thanks in advance! 回答1: If you don't want to use sentry you can use this simple middleware to attache the user-infos to the error mail: # source: https://gist.github.com/646372 class ExceptionUserInfoMiddleware(object): """ Adds user details to request context on receiving an exception, so that they show up in the error emails. Add to