/Django_Introduction_login_register

In this repository , I am gonna show you how Django works and build login , register functionality.

Primary LanguagePython

Django_Introduction_login_register

Django_basic_login

How to run this app:

  • clone this repository
  • Install requirement file
  • open directory where manage.py file is present
  • open terminal type the following command
    pip3 install -r requirement.txt
    python3 manage.py runserver

What is Django :

Django is a web development framework of python programming language. As we all know that Python programming language is not less than a treasure to a developer. It offers an abundance of libraries, packages, and framework. Django is one of them, it was developed for newspaper website so website development is easy. Django follows the Model Template View (MTV) architecture which makes it very secure. Surprisingly websites of famous organizations such as NASA, Instagram, Pinterest, and Bitbucket are developed in Django.

How to install Django :

open your terminal and paste this command
pip install Django

How to start your project:

django-admin startproject myproject

Screenshot from 2019-05-27 22-00-00

Now you can see we have created a project folder using the above command. these files created in the root directory of your project.

Manage.py file plays a very significant role in whole Django development, it helps to run the project, create superuser and many more we will talk about it later

Screenshot from 2019-05-30 01-09-44

this is the subdirectory of the project folder, here I explain you the significance of each file

  • The __init__.py file is a blank file, basically, it represents folder as a package
  • settings.py file contains the whole configuration of the Django file
  • URLs.py file is used to establish a path between views and template
  • wsgi.py file is used for app deployment on server.
Now this is all for the project folder lets move to the app section

How to create an app for your project:

First, go to the root directory and type the following command
django-admin startapp myapp

Screenshot from 2019-05-30 01-08-35.png

Now you can see our app folder created, open it and check the significance of each file

Screenshot from 2019-05-30 01-11-18

This is an app folder, here I will show you the functionality of important files

  • views.py file which is responsible for whole functionality, In this file we write our function and render them on a specific HTML page.
  • models.py file where we can specify our database structure using the class-based approach. Here we can make tables, create relation on the basis of ORM.
  • the admin.py file acts as a waiter between models and admin panel. It just makes the connection so that we can see our tables data in the admin panel

Now configure settings,py file:

step 1: Register your app in Django installed an app

Screenshot from 2019-05-30 01-00-40

step 2: Mention static Directory and templates path details

Create two folders in the root directory
  • static: it will contain our static files such as images, CSS and js
  • templates: this folder contains our HTML files for our app

Screenshot from 2019-05-30 01-14-38.png

I hope your root directory looks as described in the above picture

Screenshot from 2019-05-30 01-04-34

In this picture, I have concatenated root directory of the project with static folder

Screenshot from 2019-05-30 01-04-50

In this step, I have initialized templates folder so that our Django can easily locate it.

Screenshot from 2019-05-30 01-05-28

Here I have mentioned static files directory, It will use a lot of time in this project

Understand the functionality of MTV structure:

First understand what views.py fille does, basically it a python file which works on function here one can make function according to his own desire. Every function returns its response to the front end (HTML pages) with dynamic data.

For example, let's say we have a views.py file and have a function name demo which returns its data into an HTML page name demo.html (all HTML files should in a static folder).

views.py

def demo(request):
    username = "rajat"
    context = {
    'name': username
    }
    return render(request,'demo.html',context)

I hope you understood the functionality of views.py file, now open the HTML file and call data using Jinja2 (The Python Template Engine)

jinja 2 template call variable data in {{}} format, it also use flow control as well as conditional statements using {}.

Now open demo.html file

{% load staticfiles %}
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <h1>User name is : {{name}}</h1>
  </body>
</html>

Now our function, HTML page are ready now we need to create a separate URL for this HTML page so we need to edit urls.py of our project.

urls.py

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.demo, name = 'demo'),
    ]

Here I just configured its URL path I put empty string because I want it landing page. Suppose one just one to name it as Path('demo', views.demo) then one should open this page like this http://127.0.0.1:8000/demo.

How to create Super User in Django:

enter this command on your project root directory
python3 manage.py createsuperuser

this command created your superuser now one can run the local server and view the admin panel

python3 manage.py runserver

Screenshot from 2019-06-03 00-08-11.png

Thank you for reading this article