0

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')
halfer
  • 19,824
  • 17
  • 99
  • 186
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38
  • 2
    It looks like you are trying to roll your own authentication system. With your current experience level, you should avoid that. Use Django's auth mechanism: https://docs.djangoproject.com/en/2.1/topics/auth/ – coler-j Mar 17 '19 at 16:31
  • 1
    please format your code the exact same way as it is in your source files. The indentation here is not correct and the error you have is actually saying this. Make sure you indent your blocks as python expects it. This has nothing to do with django. – dirkgroten Mar 17 '19 at 16:40
  • 1
    Also to use `cleaned_data` you need to use a `Form`, which you haven't defined. Go through [this document](https://docs.djangoproject.com/en/2.1/topics/forms/) to learn how to create a form and use django to validate it. – dirkgroten Mar 17 '19 at 16:44
  • 1
    Note that if it's just for learning, you can define your `Member` like this, but in a real system, you would never store a password like this in a database (nor do you need to store the `confirmPass`, since it's just a validation step). – dirkgroten Mar 17 '19 at 16:47
  • Thanks all I will check it out – Nancy Moore Mar 17 '19 at 16:54
  • Thanks Sir Dirkgroten. The provided links help me get the code working for me. You can update it so that i can mark it as an answer in-case it might help someone. Great Thanks – Nancy Moore Mar 17 '19 at 20:46

1 Answers1

0

To use data_clean() method, what I did was to create a forms.py and the import it into views.py .

Here is how forms.py will look like

from django import forms

class signupForm(forms.Form):

    username = forms.CharField(max_length=30)
    password = forms.CharField(max_length=12)
    confirmPassword=forms.CharField(max_length=12)
    email=forms.CharField(max_length=30)

class loginForm(forms.Form):

    username = forms.CharField(max_length=30)
    password = forms.CharField(max_length=12)
Nancy Moore
  • 2,322
  • 2
  • 21
  • 38