DJANGO - redirect() not redirecting - appends the current path name to the domain

Viewed 1373

Basically when a POST request is made, I want to redirect to another page.

def sample(request): 
    if request.method != "POST":       
        return render(request,"user/sample.html")
    else:
        return redirect("https://www.djangoproject.com")

This code works fine when it receives a GET request but when I submit information, instead of redirecting to the page above, it appends the the template name into the url, Something like this :

http://localhost:8000/sample/sample

No matter what I type into the redirect(), even completely random things it still redirects to sample/sample

I've created multiple django-projects and in every one of them I still get this problem.

1 Answers

I've fixed the problem, leaving this here for anyone in the future. My problem was in the html file

<form action="" method="POST">
    {% csrf_token %}
    {{form}}
<input type="submit">

the <form action=""> must be an empty string, otherwise it is going to append that to the url.

The problem had nothing to do with the redirect() function.

Related