Dr. Ateendra Jha
11
Django project with user interface
Django project with user interface for registration and login. Saving user's data in encrypted form. Step by step guide.
Django project with user interface
Django framework is a very professional and widely accepted framework. Lets us understand the django project in the easy and simple words.
Initiate Django :
First need to install django
pip install django
Initiate the project
django-admin startproject <name_of_your_app>
Here initiate the project with the name of your project. it will generate the following files
yourprojectname
__init_.py
asgi.py
settings.py
urls.py
wsgi.py
manage.py
Know about each file listed above :
init.py:
This file is an empty Python script used to tell Python that the directory should be treated as a Python package. It's used to organize your project into modules and submodules.
asgi.py (Asynchronous Server Gateway Interface):
This file is used for configuring Django to work with ASGI servers, which are used to handle asynchronous web requests. ASGI is often used for real-time applications and chat systems, as it allows for more concurrency and responsiveness compared to traditional WSGI (Web Server Gateway Interface) servers.
settings.py:
This file contains the configuration settings for your Django project. You can define various settings related to your project, such as database configuration, installed apps, middleware, static and media file settings, and many others. It is a crucial file for customizing and configuring your Django project.
urls.py:
This file is used to define the URL patterns and routes for your Django project. It specifies how different URLs should be mapped to views and controllers in your project. You define URL patterns and map them to views in this file, allowing you to control the routing and behavior of your web application.
wsgi.py (Web Server Gateway Interface):
This file is used for configuring Django to work with WSGI servers, which are common in traditional web hosting environments. It allows you to deploy your Django application on web servers like Apache or Nginx using the WSGI standard.
manage.py:
This is a command-line utility for managing various aspects of your Django project. It allows you to perform tasks like creating database tables, running development servers, creating superusers, and running custom management commands. It is a convenient tool for common project management tasks and helps streamline project administration.
Initiate Application :
Inside the project we can start the application. Application is something which organise the code for reusability. It will assist to track and manage the code and related to one service. In Django, an "application" refers to a self-contained component within a Django project that serves a specific, well-defined purpose or functionality. Applications are designed to be modular, reusable, and easily pluggable into different Django projects, making it easier to organize and maintain your codebase.
Initiate the application :
Now manage.py is going to be the file to manage all the activity in django project. All the initiation will be through the manage.py . So to initiate the application,
go to the project directory where the manage.py file is and run the following
cd <path_of_project_directory>
python manage.py startapp <name_of_application>
Here as we are dealing with user, let us take name of application as users. So the code will be python manage.py startapp users .
Now we will have one more folder with users in our project directory.
Users folder will have following files
migration
__init_.py
__init_.py
admin.py
apps.py
models.py
tests.py
views.py
Now create one more file in urls.py in users folder
So now the users folser has
migration
__init_.py
__init_.py
admin.py
apps.py
models.py
tests.py
urls.py
views.py
Know about each file under application folder
init.py (Both at the application and migration level):
At the application level: This file is an empty Python script used to indicate that the application directory should be treated as a Python package.
At the migration level: This file is typically found in migration directories and is used to initialize the migration package. It's an empty file in most cases.
admin.py:
This file is used to define the admin interface for the models in your application. You can register your application's models here to make them accessible and manageable through the Django admin site. You can customize how the models are displayed and manipulated in the admin interface.
apps.py:
This file is used to configure and define application-specific settings and metadata. You can provide information such as the application name and other metadata in this file. It's also where you can define any application-specific configuration or behavior.
models.py:
This file is used to define the database models for your application. Models represent the structure of your application's data, and you define them as Python classes. Models in Django are used to interact with the database, and they define the tables and relationships within your database schema.
tests.py:
This file is used for writing unit tests for your application. You can create test cases and test the functionality of your application's views, models, and other components to ensure that they work as expected. Django provides a testing framework to help you write and run tests easily.
urls.py:
This file is used to define the URL patterns specific to your application. You can map URLs to views and control the routing of requests within your application. URLs defined in this file are often included in the project's main urls.py to create a complete URL routing system for the entire project.
views.py:
This file is used to define the views (controllers) for your application. Views are responsible for handling incoming HTTP requests, processing data, and returning appropriate responses. You define views as Python functions or classes that can interact with models and templates to generate dynamic web pages.
Working with files :
Creat the required html and pythonfiles files
mkdir users/templates
echo your_html_code_here > .\users\templates\registration.html
echo your_html_code_here > .\users\templates\profile.html
echo your_html_code_here > .\users\templates\login.html
echo your_code_here > .\users\mongoconnect.py
run following to generate the urls.py inside user directory
echo "from django.urls import path" > .\users\urls.py
echo "from . import views" >> .\users\urls.py
echo "urlpatterns = [" >> .\users\urls.py
echo " path('register/', views.register, name='register')," >> .\users\urls.py
echo " path('login/', views.login, name='login')," >> urls.py
echo " path('dashboard/', views.dashboard, name='dashboard')," >> .\users\urls.py
echo "]" >> .\users\urls.py
Now this has generated two urls one for login and one for dashboard.
Now we need to have the views for these URLs. For that we need to define the function in views.py which will be called on the respective URLs. Run following to generate the views.py inside user directory.
echo "from django.shortcuts import render, redirect" > .\users\views.py
echo "import bcrypt" >> .\users\views.py
echo "from . import mongoconnect" >> .\users\views.py
echo "" >> .\users\views.py
echo "def register(requests):" >> .\users\views.py
echo " if requests.method == 'POST':" >> .\users\views.py
echo " username = requests.POST.get('username')" >> .\users\views.py
echo " password = requests.POST.get('password')" >> .\users\views.py
echo " salt = bcrypt.gensalt()" >> .\users\views.py
echo " password = bcrypt.hashpw(password, salt)" >> .\users\views.py
echo " data = {username: password}" >> .\users\views.py
echo " mongoconnect.write_data(data)" >> .\users\views.py
echo " return render(requests, 'login.html')" >> .\users\views.py
echo "" >> .\users\views.py
echo "def login(requests):" >> .\users\views.py
echo " if requests.method == 'POST':" >> .\users\views.py
echo " username = requests.POST.get('username')" >> .\users\views.py
echo " password = requests.POST.get('password')" >> .\users\views.py
echo " salt = bcrypt.gensalt()" >> .\users\views.py
echo " password = bcrypt.hashpw(password, salt)" >> .\users\views.py
echo " login_status = mongoconnect.autheticate_user(username, password)" >> .\users\views.py
echo " if login_status == 'OK':" >> .\users\views.py
echo " redirect(requests, 'profile.html', {'user_name': username})" >> .\users\views.py
echo " else:" >> .\users\views.py
echo " redirect(requests, 'login.html')" >> .\users\views.py
Now connect the application to the django project
Go to the settings.py in project_name folder and under INSTALLED_APPS and your application. In our case it is users.
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"users",
]
Color code for the above article :
To be executed in bash/command_prompt/powershell
To be modified in files