request.POST возвращает старые значения после обновления в пользовательском промежуточном программном обеспечении - django 1.11.9
Я использую Django 1.11.9
Я хочу добавить client_id и client_secret к запросу POST django.
Вот как выглядит мой файл middleware.py:
class LoginMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
# One-time configuration and initialization.
def __call__(self, request):
# auth_header = get_authorization_header(request)
# Code to be executed for each request before
# the view (and later middleware) are called.
#Add Django authentication app client data to the request
request.POST = request.POST.copy()
request.POST['client_id'] = '12345678'
request.POST['client_secret'] = '12345678'
response = self.get_response(request)
# Code to be executed for each request/response after
# the view is called.
return response
Промежуточное программное обеспечение успешно обрабатывается, когда я проверяю его с помощью отладчика. Мысль, когда представление называется, поля client_id и client_secret отсутствуют в запросе.
После некоторых экспериментов я выясняю, что запрос не обновляется, и когда он вызывается в другом представлении, он возвращает старые значения.
Я позже использую запрос в rest_framework_social_oauth2. И это тот момент, когда "client_id" и "client_secret" исчезают.
class ConvertTokenView(CsrfExemptMixin, OAuthLibMixin, APIView):
"""
Implements an endpoint to convert a provider token to an access token
The endpoint is used in the following flows:
* Authorization code
* Client credentials
"""
server_class = SocialTokenServer
validator_class = oauth2_settings.OAUTH2_VALIDATOR_CLASS
oauthlib_backend_class = KeepRequestCore
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
import pdb ; pdb.set_trace()
# Use the rest framework `.data` to fake the post body of the django request.
request._request.POST = request._request.POST.copy()
for key, value in request.data.items():
request._request.POST[key] = value
url, headers, body, status = self.create_token_response(request._request)
response = Response(data=json.loads(body), status=status)
for k, v in headers.items():
response[k] = v
return response
Мне нужно добавить client_id и client_secret к телу запроса, чтобы его потом можно было использовать rest_framework_social_oauth2.
В чем может быть проблема? Как правильно обновить запрос?
1 ответ
Как вы работаете с request
и обрабатывая запрос, вы должны реализовать process_request
метод, поэтому результат будет примерно таким:
class LoginMiddleware(object):
def process_request(self, request):
request.session['client_id'] = '12345678'
а потом по вашему мнению:
def your_view(request):
client_id = request.session['client_id']