Home > OS >  django 4.1.1 redirect is not working if request.method=='POST'
django 4.1.1 redirect is not working if request.method=='POST'

Time:10-13

I have tried to implement redirect in django 4.1.1 views. Please find the following code.

redirect is working

def customer_registration(request):
    return redirect('customer_login')

redirect not working

def customer_registration(request):
    print("ASFADFAd")
    if request.method == 'POST':
       return redirect('customer_login')
    return render(request, 'registration/registration.html')

Can anyone help what is the problem, I have already gone through the internet none of the solutions working. Please help me.

CodePudding user response:

====== views.py ========

def HomeView(request):
    form = ProductForm()
    
    context = {
      'form':form
    }
    return render(request,'index.html',context)


def customer_registration(request):
    print("-------- GET method called ----------")
    if request.method == 'POST':
      print("-------- POST method called ----------")
      return redirect('home')

===== urls.py =====

from django.urls import path
from .views import *
 
urlpatterns = [
 
    path('home/', HomeView,name='home'),
    path('', customer_registration,name='other'),
 
]

====== html code ========

{% block body %}
     <form action="{% url 'other' %}" method="post">
          {% csrf_token %}
          
          <p>{{form.name.label}}:{{form.name}}</p>
          <p>{{form.cate.label}}:{{form.cate}}</p>
               
          <p><button type="submit">Add</button></p>
          
     </form>
{% endblock body %}

====== output after press Add button in terminal ========

-------- GET method called ----------
-------- POST method called ----------
  • Related