Django Redirect after Login Error

2024/10/11 20:31:56

Im new to Django and I know that to redirect after login I have to set the parameter 'page'. But this only works when the login is successful.

How can i do the same thing when some error occurs??

Ps: Im currently also using django-registration with simple backend

Answer

I think it's what you are looking for:

# Login
def connection(request):# Redirect to dashboard if the user is logif request.user.is_authenticated():return redirect('YourProject.views.home')# Control if a POST request has been sent.if request.method == 'POST':username = request.POST['username']password = request.POST['password']user = authenticate(username=username, password=password)if user is not None: #Verify form's content existenceif user.is_active: #Verify validitylogin(request, user)return redirect('/index') #It's ok, so go to indexelse:return redirect('/an_url/') #call the login viewreturn render(request, 'login.html', locals())  #You can remove local() it is for user's data viewing..
https://en.xdnf.cn/q/118278.html

Related Q&A

Python: Pulling .png from a website, outputting to another

Hoping this question is not too vague, or asking for too much. Essentially I am analyzing large amounts of spectra, and wanting to create one large webpage that contains these spectra rather than looki…

Using peakutils - Detecting dull peaks

Im currently using peakutils to find peaks in some data. My data contains some "dull peaks", that is my peaks plateau somewhat. I cant set my code to find these peaks even after playing aroun…

nonlinear scaling image in figure axis matplotlib

enter image description hereI hope I have not over-looked as previously asked question. I dont think so. I have an image of a spectrum. I have several laser lines for calibration. Since the laser li…

How to correctly call a git submodule symlinked?

On the Sublime Text Package Control issue:Why ignore VCS-based packages accordingly to this message? I find out what causes this error. I had the package All Autocomplete triggering it. Then I gone to…

Python and MySQL query with quotes

With a script in Python3, after extracting some strings from a file, they should be used as data to be inserted into a MySQL database as follows:query1 = """INSERT INTO {:s} VALUES ({:s}…

Using scipy kmeans for cluster analysis

I want to understand scipy.cluster.vq.kmeans. Having a number of points distributed in 2D space, the problem is to group them into clusters. This problem came to my attention reading this question and …

Scrapy and celery `update_state`

I have the following setup (Docker):Celery linked to Flask setup which runs the Scrapy spider Flask setup (obviously) Flask setup gets request for Scrapy -> fire up worker to do some workNow I wish …

SPIDEV on raspberry pi for TI DAC8568 not behaving as expected

I have a Texas Instruments DAC8568 in their BOOST breakout board package. The DAC8568 is an 8 channel, 16bit DAC with SPI interface. The BOOST package has headers to connect it to my raspberry pi, an…

Tensorflow: Simple Linear Regression using CSV data

I am an extreme beginner at tensorflow, and i was tasked to do a simple linear regression using my csv data which contains 2 columns, Height & State of Charge(SoC), where both values are float. In …

How to resolve positional index error in python while solving a condition in python?

I have the following data and I am trying the following code: Name Sensex_index Start_Date End_Date AAA 0.5 20/08/2016 25/09/2016 AAA 0.8 26/08/2016 …