In this tutorial we will finish our application, at the end of this tutorial you'll have a fully functional Django application.
We will implement :
- Forms(Login and registration)
- Template
- Full CRUD functionalities
Let's write our first form, to do so we need to create a new application and called it accounts
Run
(my-env) $ python manage.py startapp accounts
Inside accounts app create a file and call it forms.py
Now we've this tree of files :
├── accounts # new
│ ├── admin.py
│ ├── apps.py
│ ├── forms.py # new
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── contact
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── db.sqlite3
├── manage.py
├── phonebook
│ ├── asgi.py
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── Pipfile
├── Pipfile.lock
└── templates
├── base.html
├── contact
│ ├── contact_details.html
│ ├── contact_list.html
in accounts/forms.py add
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class LoginForm(forms.Form):
username = forms.CharField(
label="Username",
widget=forms.TextInput(
attrs={
"class": "form-control"
}
))
password = forms.CharField(
label="password",
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
))
class SignUpForm(UserCreationForm):
username = forms.CharField(
label="Username",
widget=forms.TextInput(
attrs={
"class": "form-control"
}
))
email = forms.EmailField(
label="Email ",
widget=forms.EmailInput(
attrs={
"class": "form-control"
}
))
password1 = forms.CharField(
label="password",
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
))
password2 = forms.CharField(
label="re-enter your password",
widget=forms.PasswordInput(
attrs={
"class": "form-control"
}
))
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
in accounts/views.py add
from django.shortcuts import render
# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from .forms import LoginForm, SignUpForm
def login_view(request):
form = LoginForm(request.POST or None)
if form.is_valid():
username = form.cleaned_data.get("username")
password = form.cleaned_data.get("password")
user = authenticate(username=username, password=password)
if user is not None:
login(request, user)
return redirect("/contacts/")
return render(request, "accounts/login.html", {"form": form})
def register_user(request):
if request.method == "POST":
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get("username")
raw_password = form.cleaned_data.get("password1")
user = authenticate(username=username, password=raw_password)
return redirect("/login/")
else:
form = SignUpForm()
return render(request, "accounts/register.html", {"form": form})
Create urls.py and add
from django.urls import path
from .views import login_view, register_user
from django.contrib.auth.views import LogoutView
urlpatterns = [
path('login/', login_view, name="login"),
path('register/', register_user, name="register"),
path("logout/", LogoutView.as_view(), name="logout")
]
Don't forget to update your main urls.py
from django.contrib import admin
from django.urls import path, include # add this
urlpatterns = [
path('admin/', admin.site.urls),
path("", include("contact.urls")), # add this
path("", include("accounts.urls")) # add this
]
Now you understand how Django works, here is the full application on Github
Read more about Django Here!
Thanks for reading!
Top comments (5)
Nice work. The boilerplate is mentioned in a related article published here on Dev:
Django Boilerplate Code - Open-Source and Free
Cheers!
Thank you, Very helpful!
Hi Diop,
May I ask about the 'class Meta' in the forms.py file? What its used for?
Thank you.
Hi dear,
Thank you for reading.
In the Django web framework, a meta class is used to define an extra option for a forms so that other classes withing the web app know the capabilities of the model.
Here is an interesting article about it clouditate.com/what-is-a-meta-clas...
Thank you so much