Q:

if statement is not working, it is automatically using else statement

from django.shortcuts import render, redirect

from django.http import HttpResponse

from django.contrib.auth.models import User, auth

def register(request):

     if request.method == 'POST':

         first_name = request.POST['first_name']
         last_name = request.POST['last_name']
         username = request.POST['username']
         email = request.POST['email']
         password1 = request.POST['password1']
         password2 = request.POST['password2']
         user = User.objects.create_user(username=username, password=password1,email=email,first_name=first_name,last_name=last_name)
         user.save()
         return render(request, 'register.html')

     else:
        return HttpResponse('run')

A:

You probably need to initialize your form object, which would have been defined in forms.py by convention.
For example:
from .forms import ContactDetailsForm

def home_view(request):
    """The Home Page"""

    # Initiate forms
    form = ContactDetailsForm(request.POST or None)

    # check request
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            redirect('register')
    else:
        redirect('home')

In this example, ContactForm would be defined in forms.py, by convention
# in forms.py
from .models import ContactDetail

class ContactDetailsForm(forms.ModelForm):
    name = forms.CharField(max_length=20, label='')
    email = forms.EmailField(label='')

    class Meta:
        model = ContactDetail # model used from models.py

And, by convention, ContactDetail would be imported from models.py, where it was defined as:
# in models.py

class ContactDetail(models.Model):
    name = models.CharField(max_length=20)
    email = models.EmailField(default='', verbose_name='Preferred Email')

Of course your forms and models and views will likely be more complicated than that. This is just an example.

