Python-Django Framework Tutorial Part-1

Python-Django Framework

Posted by indranil on March 9, 2021, 10:12 p.m.

Setup your Enviroment for your Django-Project. In this previous post i already told you how to setup your Enviroment and what basic needed for create django-webapp. If you dont know how to setup Enviroment then visit https://coder-i.com/django-python-tutorial

Create a Folder django-project

Then Open It On Visual-studio-Code Or Pycharm

Open Terminal

django-admin startproject djangoproject .
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser


Enter Your details

python manage.py runserver

Click This Link 127.0.0.1:8000

Now Login your admin panel
Enter your superuser Id and password

Click This Link 127.0.0.1:8000/admin/

how to  to create django webapp and manage it?

What’s the difference between a project and an app? An app is a Web application that does something – e.g., a Weblog system, a database of public records or a small home app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

Your apps can live anywhere on your Python path. In this tutorial, we’ll create our home app in the same directory as your manage.py file so that it can be imported as its own top-level module, rather than a submodule of mysite.

To create your app, make sure you’re in the same directory as manage.py and type this command: 

python manage.py startapp home

Your django-project stracture like 

django-project/
    manage.py
    djangoproject/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

Your home app stracture like 


home/
__init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py

Now add urls.py file on your home app

Lets get to create pipeline to attach your app on your django-project

Here you must be know about settings.py and urls.py

django-project/
    manage.py
    djangoproject/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

settings.py file is a main file for your django-project. here you can manage your all project settings. But in this time we need to link our home app on django-project. Lets open settings.py file on your texteditor or vs code

Find The INSTALLED_APPS section

#Add your home app like that. If your app name like test then you should be add 'test.apps.TestConfig' .

INSTALLED_APPS = [

    'home.apps.HomeConfig',

]

 Open urls.py file on your django-project folder. this file will be access your all apps urls.py file link.

Directory django-project/djangoproject/urls.py 

#Replace this line
from django.urls import path, include

Find The urlpatterns section


#Add your home app like that. If your app name like test then you should be add path('', include('test.urls') ),  .
urlpatterns = [

    path('', include('home.urls') ),

]

Now Open your app's url

Directory django-project/home/urls.py

#Add All this script 
from django.urls import path

from . import views

urlpatterns = [

   path('', views.homeIndex, name='homeIndex'),

]

Now Open your app's views.py


Directory django-project/home/views.py

 What is views.py file?

A view function, or view for short, is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image . . . or anything, really. The view itself contains whatever arbitrary logic is necessary to return that response. This code can live anywhere you want, as long as it’s on your Python path. There’s no other requirement–no “magic,” so to speak. For the sake of putting the code somewhere, the convention is to put views in a file called views.py, placed in your project or application directory.

views.py

from django.shortcuts import HttpResponse

# Create your views here.

def homeIndex(request):

   return HttpResponse('This is Home Index')

Now Run Your Server On Terminal

python manage.py runserver

How to Create Multyple pipeline or link on our project?

Now Open your app's url

Directory django-project/home/urls.py

urls.py

from django.urls import path

from . import views

urlpatterns = [

    path('', views.homeIndex, name='homeIndex'),

    path('Test/', views.testIndex, name='testIndex'),

    path('Test-1/', views.test1Index, name='test1Index'),

    path('Test-2/', views.test2Index, name='test2Index'),

]

Now Open your app's views

Directory django-project/home/views.py

views.py

from django.shortcuts import render, redirect, HttpResponse

# Create your views here.

def homeIndex(request):

    return HttpResponse('ok')

def testIndex(request):

    return HttpResponse('Test')

def test1Index(request):

    return HttpResponse('Test1')

def test2Index(request):

    return HttpResponse('Test-2')

How to Create Simple function on views.py?

views.py

from django.shortcuts import render, redirect, HttpResponse

# Create your views here.

def homeIndex(request):

    return HttpResponse('ok')

#Simple Date time function

import datetime #required module

def testIndex(request):

    now = datetime.datetime.now()

    html = "<html><body>It is now %s.</body></html>" % now

    return HttpResponse(html)

Related Question:

How to render html page on our django-project?

What Is static file? how to i used static file on our project?

What is models? how to create a simple database?

what is media? how to use media on our project?

 

noimg Visit Link: digitalocean.com