Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the result on same page in django?

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"

like image 790
cold_coder Avatar asked Nov 07 '14 10:11

cold_coder


People also ask

How do I return a redirect in Django?

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/ .

What is return render in Django?

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").


1 Answers

Use return redirect(request.META['HTTP_REFERER']) to redirect to previous url.

like image 139
Geo Jacob Avatar answered Sep 28 '22 02:09

Geo Jacob