How to redirect django.contrib.auth.views.login after login?

2024/11/15 10:14:52

I added django.contrib.auth.views.login everywhere in my webpage, for that I had to load a templatetag (that returns the AuthenticationForm) in my base.html. This templatetags includes the registration/login.html template.

The login is working ok but I want it to redirect the users to the same page they are before login. Now, it redirects me to /wherever_i_am/login wich shows registration/login.html with the 'login ok' or 'login fails' messages but without the rest of base.html.

I have followed django documentation and a few SO questions like this but I cannot redirect correctly. I have modified the next variable but it doesn't seem to work (next={{ request.get_full_path }} redirects me to /wherever_i_am/login ...again)

Have you tried something similar? any ideas?

UPDATE1 Now, the question could be something like: Do I have to declare my own login view if I want to include the login form everywhere in my web page?

Thank you.

Answer

Found answer:

Change settings.LOGIN_REDIRECT_URL in your settings.py,

below code is copy from django:

   if request.method == "POST":form = authentication_form(data=request.POST)if form.is_valid():# Ensure the user-originating redirection url is safe.if not is_safe_url(url=redirect_to, host=request.get_host()):redirect_to = settings.LOGIN_REDIRECT_URL...
https://en.xdnf.cn/q/71966.html

Related Q&A

How to do windows API calls in Python 3.1?

Has anyone found a version of pywin32 for python 3.x? The latest available appears to be for 2.6.Alternatively, how would I "roll my own" windows API calls in Python 3.1?

Returning the outputs from a CloudFormation template with Boto?

Im trying to retrieve the list of outputs from a CloudFormation template using Boto. I see in the docs theres an object named boto.cloudformation.stack.Output. But I think this is unimplemented functi…

numpy.array of an I;16 Image file

I want to use TIFF images to effectively save large arrays of measurement data. With setting them to mode="I;16" (corresponding to my 16 bit data range), they yield 2MB files (~1000x1000 &quo…

Namespace packages and pip install -e

I have a ns.pkg2 package that depends on ns.pkg1 package. I make a fork of it, publish it to git and want to install my version into my virtualenv. I use pip install -e mygit and end up with ns.pkg in …

Python sys.argv out of range, dont understand why

I have a script that Ive been using for a some time to easily upload files to my server. It has been working great for a long time, but I cant get it to work on my new desktop computer. The code is sim…

Error calling BashOperator: Bash command failed

Here are my dag file and BashOperator task:my_dag = { dag_id = my_dag, start_date = datetime(year=2017, month=3, day=28), schedule_interval=01***, }my_bash_task = BashOperator( task_id="my_bash_t…

Match unescaped quotes in quoted csv

Ive looked at several of the Stack Overflow posts with similar titles, and none of the accepted answers have done the trick for me.I have a CSV file where each "cell" of data is delimited by …

Creating RDF file using csv file as input

I need to convert a csv file to rdf with rdflib, I already have the code that reads the csv but I do not know how to convert it to rdf.I have the following code:import csv from rdflib.graph import Grap…

Explicit vertex position in python graph-tool

I am using python graph-tool. To draw graphs, it uses graph_draw function. I want to send vertex positions explicitly to dot engine. It turns out that I can pass a property map named pos. I tried defin…

Add jar to pyspark when using notebook

Im trying the mongodb hadoop integration with spark but cant figure out how to make the jars accessible to an IPython notebook.Here what Im trying to do:# set up parameters for reading from MongoDB via…