N
N
Night Tarlis2016-09-06 15:28:35
Django
Night Tarlis, 2016-09-06 15:28:35

How to do redirect after sending response?

There is a view that receives a parameter, you need to check if this parameter is in the cookie:
a) redirect
b) if not, then add to cookies and redirect ... How to combine a redirect and sending a response ...

def redirect_to_basic_url(request, arg_one):
    obj = get_object_or_404(Model, model_param=arg_one)
    if str(arg_one) in str(request.COOKIES.get('cookie_name')):
        return redirect(obj.PARAM)
    else:
        context = {'short_cookie': str(request.COOKIES.get('cookie_name'))}
        context['short_cookie'] += ' | ' + str(arg_one)
        response = render(request, 'template.html', context)
        response.set_cookie('cookie_name', context['short_cookie'])
        return response #and redirect(obj.PARAM)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2016-09-06
@tarlis

response = redirect(obj.PARAM)
response.set_cookie('cookie_name', context['short_cookie'])
return response

U
un1t, 2016-09-06
@un1t

Something like this:
...
response['Location'] = '/myurl/'
response.status_code = 302
return response

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question