I am learning Django for the first time. Here am trying to submit form data to the database and the code is working great.
Here is my views.py
from django.shortcuts import render, redirect, HttpResponseRedirect
from .models import Member
# Create your views here.
#proces form data
def index(request):
if request.method == 'POST':
member = Member(username=request.POST['username'], password=request.POST['password'], confirmPassword=request.POST['confirmPassword'], email=request.POST['email'])
member.save()
return redirect('/')
else:
return render(request, 'web/index.html')
Here is models.py
from django.db import models
# Create your models here.
class Member(models.Model):
username=models.CharField(max_length=30)
password=models.CharField(max_length=12)
confirmPassword=models.CharField(max_length=12)
email=models.CharField(max_length=30)
def __str__(self):
return self.username + " " + self.email
Here is index.html
{% extends 'web/base.html' %}
{% block body %}
<form method="POST">
{% csrf_token %}
<input type="text" name="username" id="username"/>
<input type="password" name="password" id="password"/>
<input type="password" name="confirmPassword" id="confirmPassword"/>
<input type="text" name="email" id="email"/>
<button type="submit"> Submit</button>
</form>
{% endblock %}
Here is what I want to add:
Now I want to validate data before submission using cleaned_data[] method and also check that the password and confirm password matched prior to form submission but am getting the error below
identationError; expected an idented block.
I have referenced solution found here but with no luck link
Here is what I have tried to that effect:
#proces form data
def index(request):
if request.method == 'POST':
uname = request.cleaned_data['username']
pass = request.cleaned_data['password']
confirmPass = request.cleaned_data['confirmPassword']
email = request.cleaned_data['email']
if pass != confirmPass:
context = {'msg': 'Passwords does not match'}
return render(request, 'web/error.html', context)
#insert records if things were okay
member = Member(username=uname, password=pass, confirmPassword=confirmPass, email=email)
member.save()
return redirect('/')
else:
return render(request, 'web/index.html')