Online Survey Tool using Django
Last Updated :
07 Mar, 2024
In this article, we’ll help you make an Online Survey Tool using Django. In our tool, only admin users can create forms to keep things in control and ensure quality. But, we’ve also made it so that authenticated users can fill out surveys. If you’re not logged in, you can’t submit a survey anonymously. Once you’re logged in, you can see your survey answers and others too. Just remember, users can look at the responses, but they can’t change any data unless the admin says it’s okay. This setup makes sure the survey space is safe and organized, keeping a balance between user access and data safety.
Online Survey Tool using Django
Below is the step-by-step implementation of the Online Survey Tool using Django Framework in Python:
Starting the Project Folder
To start the project use this command
django-admin startproject survey_project
cd survey_project
To start the app use this command
python manage.py startapp survey_app
Now add this app and djf_surveys to the ‘settings.py’
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djf_surveys',
'survey_app',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
-----------------------------------------------
'django.contrib.messages.context_processors.messages',
'djf_surveys.context_processors.surveys_context' # new
],
},
},
]
Install the django-form-surveys library also which is used for create Survey Tool using the following command.
pip install django-form-surveys
File Structure
file structure
Setting Necessary Files
views.py : This Django code handles user authentication, login, logout, registration, and success page rendering. The `index` function redirects authenticated users based on stored data, `login` manages user login, `logout` logs users out, `register` handles user registration, and `success` renders a success page.
Python3
from django.shortcuts import render, redirect
from django.contrib.auth.models import User, auth
from django.contrib import messages
def index(request):
if request.user.is_authenticated:
full_uri = request.session.get( 'full_uri' , None )
if full_uri is None :
return redirect( 'success' )
else :
del request.session[ 'full_uri' ]
return redirect(full_uri)
else :
return redirect( 'login' )
def login(request):
if request.method = = 'POST' :
username = request.POST[ 'username' ]
password = request.POST[ 'password' ]
user = auth.authenticate(username = username, password = password)
if user is not None :
auth.login(request, user)
return redirect( 'index' )
else :
messages.info(request, 'Invalid credentials' )
return render(request, 'login.html' )
else :
print ( "Hello" )
request.session[ 'full_uri' ] = request.build_absolute_uri( "/surveys/" )
print (request.session[ 'full_uri' ])
return render(request, 'login.html' )
def logout(request):
auth.logout(request)
return redirect( 'login' )
def register(request):
if request.method = = 'POST' :
username = request.POST[ 'username' ]
password = request.POST[ 'password' ]
password2 = request.POST[ 'password2' ]
if password = = password2:
if User.objects. filter (username = username).exists():
messages.info(request, 'Username taken' )
return redirect( 'register' )
else :
user = User.objects.create_user(username = username, password = password)
user.save()
return redirect( 'login' )
else :
messages.info(request, 'Passwords do not match' )
return redirect( 'register' )
else :
return render(request, 'register.html' )
def success(request):
return render(request, 'success.html' )
|
project/urls.py : This Django code configures URL patterns for the application. The admin
path directs to the Django admin interface. The empty path and ‘surveys/’ path include URLs from ‘survey_app.urls’ and ‘djf_surveys.urls’.
Python3
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path( 'admin/' , admin.site.urls),
path(' ', include(' survey_app.urls')),
path( 'surveys/' , include( 'djf_surveys.urls' ))
]
|
app/urls.py: This Django code defines URL patterns for views in the application. The empty path corresponds to the ‘index’ view, ‘accounts/login/’ leads to the ‘login’ view, ‘logout’ directs to the ‘logout’ view, ‘register’ connects to the ‘register’ view, and ‘success’ corresponds to the ‘success’ view
Python3
from django.urls import path
from . import views
urlpatterns = [
path(' ', views.index, name=' index'),
path( 'accounts/login/' , views.login, name = 'login' ),
path( 'logout' , views.logout, name = 'logout' ),
path( 'register' , views.register, name = 'register' ),
path( 'success' , views.success, name = 'success' ),
]
|
Creating GUI for Online Survey Tool
templates/login.html : This HTML code creates a login page for a Django web app using Tailwind CSS. It displays error messages, includes a form for username and password, and provides a link to the registration page. The styling is done with Tailwind CSS classes, and the form action is set to the ‘login’ URL, ensuring users can log in securely.
HTML
<!DOCTYPE html>
< html >
< head >
< title >Login</ title >
</ head >
< body >
{% for message in messages %}
< p style = "color: red;" >{{ message }}</ p >
{% endfor %}
< div class = "flex flex-col justify-center items-center min-h-screen leading-loose" >
< h1 class = "block mb-3 font-semibold text-xl" >Login</ h1 >
< form method = "POST" action = "{% url 'login' %}" class = "w-full max-w-xs" >
{% csrf_token %}
< label for = "username" class = "font-semibold block" >Username:</ label >
< input type = "text" placeholder = "Enter username" class = "bg-gray-100 text-black-700 p-2 rounded border w-full" id = "username" name = "username" required autofocus>
< label for = "password" class = "font-semibold block mt-4" >Password:</ label >
< input type = "password" placeholder = "Enter password" class = "bg-gray-100 text-black-700 p-2 rounded border w-full" id = "password" name = "password" required>
< input type = "submit" class = "bg-blue-500 hover:bg-blue-800 text-white font-semibold p-2 rounded w-full mt-4" value = "Login" >
< p class = "mt-4" >Don't have an account? < a href = "{% url 'register' %}" class = "text-blue-500 hover:text-blue-900 font-semibold" >Register</ a ></ p >
</ form >
</ div >
</ body >
</ html >
|
templates/register.html : This HTML code creates a registration page for a Django web app with Tailwind CSS styling. It displays error messages, includes a form for username and password, and provides a link to the login page. The form action is set to the ‘register’ URL, ensuring secure user registration.
HTML
<!DOCTYPE html>
< html >
< head >
< title >Register</ title >
</ head >
< body >
{% for message in messages %}
< p style = "color: red;" >{{ message }}</ p >
{% endfor %}
< div class = "flex flex-col justify-center items-center min-h-screen leading-loose" >
< h1 class = "block mb-3 font-semibold text-xl" >Register</ h1 >
< form method = "POST" action = "{% url 'register' %}" class = "p-5 w-full md:max-w-lg" >
{% csrf_token %}
< label for = "username" class = "font-semibold block" >Username:</ label >
< input type = "text" placeholder = "Enter username" class = "bg-gray-100 text-black-700 p-2 rounded border w-full" id = "username" name = "username" required autofocus>
< label for = "password" class = "font-semibold block mt-4" >Password:</ label >
< input type = "password" placeholder = "Enter password" class = "bg-gray-100 text-black-700 p-2 rounded border w-full" id = "password" name = "password" required>
< label for = "password2" class = "font-semibold block mt-4" >Confirm Password:</ label >
< input type = "password" placeholder = "Enter password" class = "bg-gray-100 text-black-700 p-2 rounded border w-full" id = "password2" name = "password2" required>
< input type = "submit" class = "bg-blue-500 hover:bg-blue-800 text-white font-semibold p-2 rounded w-full mt-4" value = "Register" >
< p class = "mt-4" >Already have an account? < a href = "{% url 'login' %}" class = "text-blue-500 hover:text-blue-900 font-semibold" >Login</ a ></ p >
</ form >
</ div >
</ body >
</ html >
|
templates/success.html: This HTML code defines a success page for a Django web application using Tailwind CSS styling. The page includes a title, a success message, and an animated pulsating circle to signify completion.
HTML
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Success Page</ title >
< style >
@keyframes beat {
0% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
.beat-animation {
animation: beat 1s infinite;
}
</ style >
</ head >
< body >
< div class = "flex flex-col items-center justify-center h-screen" >
< h1 class = "text-4xl font-bold mb-4" >Success!</ h1 >
< p class = "text-lg mb-8" >You are good to go with the URL now.</ p >
< div class = "w-16 h-16 bg-blue-500 rounded-full animate-pulse beat-animation" ></ div >
</ div >
</ body >
</ html >
|
Deployment of the Project
Run these commands to apply the migrations:
python3 manage.py makemigrations
python3 manage.py migrate
Run the server with the help of following command:
python3 manage.py runserver
Output
Video Demonstration
Share your thoughts in the comments
Please Login to comment...