How to return the result on same page in django?
Use return redirect(request.META['HTTP_REFERER'])
to redirect to previous url.
You can make use of Django messages. visit https://docs.djangoproject.com/en/2.1/ref/contrib/messages/ I have given an example of login functionality.
In views.py
class LoginView(View):
def post(self, request,*args, **kwargs):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
messages.success(request, 'woahh!! logged in..')
return redirect('home')
else:
return HttpResponse('please! verify your Email first')
else:
messages.error(request, 'username or password not correct')
return redirect('login')
In templates/login.html
<form method="post"v action="{% url 'login' %}">
{% csrf_token %}
<input type="text" name="username" placeholder="Username">
<input type="password" name="password" placeholder="Password">
{% for error in messages %}
<p>{{ error }}</p><br>
{% endfor %}
<button type="submit" >Login</button>
</form>
Hope this can help you.
I think you are looking for user login functionality, you can reffer django user login here And also use django Message framework to display messages
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect
def my_view(request):
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Return a 'disabled account' error message
return redirect('/')
else:
# Return a 'disabled account' error message
return redirect('/')
else:
# Return an 'invalid login' error message.
return redirect('/')