Getting started with Django 3.0 part 4
Tags: python, django
Published: 2019-12-25 09:35:46

---
title: Getting started with Django 3.0 part 4
published: true
description: "Build your first application with Django"
tags: python, django
cover_image: https://images.unsplash.com/photo-1576306686882-c3cb97a70366?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=334&q=80
---

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

```sh
(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 :

```sh
├── 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

```sh
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

```sh
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

```sh
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**

```sh
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](https://github.com/xarala221/django-phonebook.git)

Read more about Django [Here](https://docs.djangoproject.com/en/3.0/)!

Thanks for reading!