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

Handling Next in Login

The next attribute in the url can be handled inside the login function to navigate the user to the page he actually wanted to visit to instead of redirecting him to the default home page.

For example, lets create a demo page with a url http://127.0.0.1:8000/demo, which requires authentication.

First you can create a demo.html file inside the templates folder and add the following code.

<!DOCTYPE html>
<html>
<head>
  <meta charset=" utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Demo</title>
</head>
<body>
    <h1>Demo Page</h1>
    <h2>Welcome {{request.user}}</h2>
    <h2>Restricted content to be accessed by only authenticated users</h2>

    <a href="{% url 'logout' %}">Logout</a>
</body>
</html>

Now lets create a function to render this template and map it with a url.

Add a function in the authentication/views.py file :

@login_required(login_url='login')
def demo(request):
    return render(request, 'demo.html')

Also map it with a url in authentication/urls.py file :

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

After doing this when you try to visit http://127.0.0.1:8000/demo, you will be redirected to the login page along with the next attribute set to /demo.

alt text The next attribute can be handled in the login function to navigate it to the particular page after logging in. Edit the login function as follows to handle the next attribute :

def user_login(request):
    if request.method == 'POST':
        next = request.GET.get('next')
        form = AuthenticationForm(request, data=request.POST)
        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)
                if next:
                    return redirect(next)
                return redirect('home')
    else:
        form = AuthenticationForm()
    
    return render(request, 'login.html', {'form': form})

If the next attribute is present, it will redirect it to the particular page, else it will redirect the user to the home page.

In our case, it will redirect the user to the demo page after login : alt text