How to create a simple registration form in django?

by raul_reichert , in category: Python , 2 years ago

How to create a simple registration form in django?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by roberto , a year ago

@raul_reichert To create a simple registration form in Django, you'll need to do the following:

  • In your myapp/models.py file, define a model for your user registration form. For example:
1
2
3
4
5
6
7
8
from django.db import models

class User(models.Model):
  username = models.CharField(max_length=100)
  password = models.CharField(max_length=100)
  email = models.EmailField()
  first_name = models.CharField(max_length=100)
  last_name = models.CharField(max_length=100)


In your myproject/settings.py file, add your app to the list of INSTALLED_APPS:

1
INSTALLED_APPS = [  ...  'myapp',]

Run the following commands to create the database tables for your app:

1
2
$ python manage.py makemigrations
$ python manage.py migrate


In your myapp/views.py file, create a view for your registration form. This view will handle rendering the form and processing user input when the form is submitted. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django.shortcuts import render
from django.http import HttpResponseRedirect
from .models import User

def register(request):
  if request.method == 'POST':
    # process user input
    username = request.POST['username']
    password = request.POST['password']
    email = request.POST['email']
    first_name = request.POST['first_name']
    last_name = request.POST['last_name']
    user = User(username=username, password=password, email=email, first_name=first_name, last_name=last_name)
    user.save()
    return HttpResponseRedirect('/success/')
  else:
    # render the form
    return render(request, 'myapp/register.html')


Create a template for your registration form in the myapp/templates/myapp directory. This template should include a form that uses the POST method and has fields for each of the user attributes you defined in your model (username, password, email, first name, last name). For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<h1>Register</h1>
<form method="post">
  {% csrf_token %}
  <label>Username:</label><br>
  <input type="text" name="username"><br>
  <label>Password:</label><br>
  <input type="password" name="password"><br>
  <label>Email:</label><br>
  <input type="email" name="email"><br>
  <label>First name:</label><br>
  <input type="text" name="first_name"><br>
  <label>Last name:</label><br>
  <input type="text" name="last_name"><br><br>
  <input type="submit" value="Submit" />
</form>
by kellie.bechtelar , 10 months ago

@raul_reichert 

To create a simple registration form in Django, follow the steps below:


Step 1: Create a Django project and app


First, create a new Django project and app by running the following commands:

1
2
3
django-admin startproject myproject
cd myproject
python manage.py startapp accounts


Step 2: Create a registration form model


In the accounts/models.py file, create a User model with the necessary fields for registering a user. For example:

1
2
3
4
5
6
7
from django.db import models

class User(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    email = models.EmailField()
    password = models.CharField(max_length=50)


Step 3: Create a registration form view


In the accounts/views.py file, create a view function for rendering and processing the registration form. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
from django.shortcuts import render, redirect
from .forms import RegistrationForm

def registration(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:
        form = RegistrationForm()
    return render(request, 'registration.html', {'form': form})


Step 4: Create a registration form template


In the accounts/templates directory, create a registration.html template that includes the registration form. For example:

1
2
3
4
5
6
7
<h1>Registration Form</h1>

<form method="post">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Register</button>
</form>


Step 5: Create a registration form URL


In the myproject/urls.py file, create a URL pattern for the registration form view. For example:

1
2
3
4
5
6
from django.urls import path
from accounts.views import registration

urlpatterns = [
    path('registration/', registration, name='registration'),
]


Step 6: Create a registration form class


In the accounts/forms.py file, create a RegistrationForm class with the necessary fields and validation rules. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
from django import forms
from .models import User

class RegistrationForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['first_name', 'last_name', 'email', 'password']

    def clean_email(self):
        email = self.cleaned_data['email']
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError('Email already exists')
        return email


That's it! You now have a simple registration form in Django. When the form is submitted, a new User object will be created and saved to the database.