Reputation: 1324
I have a custom signup page and I would like to send email verification after user creation. However, I would like to redirect users to a different template after signup which shows them a message that they need to verify their email address.
Currently my view is:
def signup(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
user = form.save(commit=False)
#user.is_active = True
user.save()
send_email_confirmation(request, user, True)
else:
form = SignUpForm()
return render(request, 'main/signup.html', {'form': form})
How can I redirect users to a the template verification_sent.html
? I have also implemented a method for users to change the email address if incorrect but I cannot find how I can integrate that in my verification_sent.html
template.
Upvotes: 1
Views: 293
Reputation: 3527
In this example we use ajax to send the user registration data to the backend. If the backend successfully registers a new user we return a response object. Upon retrieving the response object, js will redirect the user to the verification page.
script.js:
function register() {
// conduct ajax request:
$.ajax({
url : 'register',
data : {
csrfmiddlewaretoken : 'the_token',
username : 'the_username',
password : 'the_password',
},
success : register_success, // reference to function below:
})
} $('#register').click(register); // execute function when register button is clicked
// executes upon retrieving a successful response from backend:
function register_success(response) {
// unpack response:
status = response.status;
// redirect users with okay status:
if (status=='okay') window.location('verification');
}
views.py:
def register(request):
# unpack request:
username = request.POST['username']
password = request.POST['password']
# register user, send email:
...
# pack response:
response = json.dumps({
'status' : 'okay' # or other status for failed registration attempts
})
return HttpResponse(response)
Upvotes: 1