Django

College Event Registration Website

CampusConnect Introduction and Setup Configuring settings file for template, static and media files Setting up Tailwind Creating Custom User Model Creating Super User for CampusConnect Registering Custom User Model Search and Filter for User Model Serving Media Files During Development Categorizing Departments Linking Department with HOD Creating Abstract Model for Event Creating Model for Workshop Customizing Admin Page for Workshop Update in Model AbstractEvent Adding Instructor for Workshop Instructor Model Admin Page Adding Poster Field in Abstract Event Providing Access to HOD Access Update for HOD Restricting HOD Access to Particular Department AbstractEvent On Spot Registration Field Creating Workshop Object Creating and Linking Home Page Displaying Workshop on Home Page Styling Home Page Adding Workshop Detail Page Link Workshop Detail Page Workshop Detail Page Styling Workshop Instructor Details Workshop Detail Contact Contact Admin Page Many to Many Field for Contact Displaying Contact on Workshop Detail Page Adding Title for Workshop Detail Page Adding Gallery for Workshop Workshop Gallery Admin Page Displaying Gallery Images on Website Through Context Displaying Gallery Images on Website through template tags Authentication for users User Registration User Registration Submission Logout Functionality For User Login Functionality for User Model For Workshop Registration Workshop Registration Admin Page Register Workshop Function Register Button in Workshop Page Validations Before Workshop Registration Workshop Registration Closed Validaiton User Already Registered for Workshop Validation Workshop Registration Report From Admin Page Export using Library in Django Admin Extending Abstract Event for Hackathons

Register User

You can register or create new users in django for the User Model as follows.

First, you need to create the User Model. This can be done by running the python manage.py migrate command. All the default django models would be created when you run this command.

python manage.py migrate

alt text

Next open the authentication/views.py file and add the code to create a new User. You can use the UserCreationForm that django forms offers in order to create a new user. That form would be passed along the template when a GET request is called to the url. On a POST request, the form data would be saved and the user would be logged by using the login() function from django auth module, and then the user would be redirected to the home page.

from django.shortcuts import render, redirect
from django.contrib.auth import login
from django.contrib.auth.forms import UserCreationForm

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('home')
    else:
        form = UserCreationForm()
    return render(request, 'register.html', {'form': form})

Also, lets create a route for the register function, edit the authentication/urls.py as follows :

path("register", views.register,name='register'),

Along with that, you should create a register.html template in order to handle the form. You can display the UserCreationForm as a paragraph in the html template as follows. The form method is set to POST, so that it sends a POST request when the form is submitted. The {% csrf_token %} should be present to ensure that the form is submitted through our website and not from any other source.

<!DOCTYPE html>
<html>
<head>
  <meta charset=" utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Register</title>
</head>
<body>
    <form method="post">
        {% csrf_token %}
        {{ form.as_p }}
        <button type="submit">Register</button>
    </form>
</body>
</html>

Now when you visit http://127.0.0.1:8000/register, you will see a web page as follows. alt text By entering the details, it will allow you to create a new user. alt text Click the register button, in order to create a new user. After the register button is clicked it will login the user and redirect to the home page.

alt text In order to verify which user is logged in, you can display the user in the home.html page as follows :

<h1>Home Page</h1>
<h2>Welcome {{request.user}}</h2>
<h2>Restricted content to be accessed by only authenticated users</h2>

Now when you open the website, you can see the username along with it. alt text Also, if you try to create a new user with the same username, it will show an error. alt text These validations are handled by django internally along with the password validation. The password validation is based on the AUTH_PASSWORD_VALIDATORS field in the settings.py file. The default value of the field would be like this :

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

You can remove or add some validations by editing the AUTH_PASSWORD_VALIDATORS in settings.py file.

For example, if you want to allow that a password can be entirely numeric, then you can remove the NumericPasswordValidator from the list.

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
]

Now the website would be as follows : alt textAs you can see the text that Your password can't be entirely numeric is now not present. Now you can create a new user whose password can even be entirely numeric.