How to get the result of a particular query on same page in django ? For example if my "index.html" has a form with two fields one for username and password. In views.py I check whether the values matches to fields in db or not. I want to return a string "Login successfull" or "Login Failed" same page. Please suggest the necessary code for views.py and index.html. Currently I check the values and render a new page.Please suggest how to return it on same page. Thanks in advance!
def index(request):
return render(request, "index.html")
def stage2(request):
if Verify.objects.filter(usr=request.POST['us'],password=request.POST['pass'])
request.session['usr']=request.POST['us']
a=request.session['usr']
dict1={'u':a}
return render(request,"success.html",dict1)
else:
return render(request,"failed.html")
I want to show the result on same page i.e "index.html"
Just call redirect() with a URL in your view. It will return a HttpResponseRedirect class, which you then return from your view. Assuming this is the main urls.py of your Django project, the URL /redirect/ now redirects to /redirect-success/ .
This function uses the render() function to create the HttpResponse that is sent back to the browser. This function is a shortcut; it creates an HTML file by combining a specified HTML template and some data to insert in the template (provided in the variable named "context").
Use return redirect(request.META['HTTP_REFERER'])
to redirect to previous url.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With