Django StartGuide

Django Kick-start

Content

1. Start your project.

2. Create your first view

3. Use templates



1. Start your project.

Django is used to create a light weight webapps, to create a REST APIs and expose them using the HTTP ports


Use the below git repo to kickstart your project

git clone https://github.com/LinkedInLearning/django-esst-2894047.git

checkout the branch 01-01b

git checkout 01-01b

git pull

 

Note - Assuming your local .git.config file is set up in your local system

 

#1 command to create a project with name "smartnotes"

django-admin startproject smartnotes .

 

This above command will create two files manage.py and a directory with project name in the root dir.

 

#2 To create a server, use the below command from the root directory 


python3 manage.py  runserver

Note - Ignore the warnings as of now and checkout the url "http://127.0.0.1:8000/"

a sever is running on localhost and port 8000

 

Congrats !  you have create a django sever in your local machine
Later we will see, how to integrate or run the same in AWS or Azure native cloud



#3 This is to create a app inside your project (Django has provided this capability to decouple the code/functionality to make it work like micro services or app)


django-admin startapp home

**after this command, a new directory named home will be created in the project root directory, we need to mention this home app in the settings.py file which is inside our smartnotes directory

** we have to mention this home app in the INSTALLED_APPS section in the setting.py file.


INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

#apps
'home',
]

 

This would look something like this

since our project template is created and the app is configured, Now's it time to start with our first function in the views.py file (which is used to create the REST API or any function that we are exposing in our webapp)


2. Create your first view

Make a change in your views.py file which is a part of your home app

from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def home(request):
return HttpResponse('Hello Django')
  


After adding this function in the views, connect it to your url.py file by making a below entry 

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

from home import views

urlpatterns = [
path('admin/', admin.site.urls),
path('home', views.home)


This will create a url in your server with the name /home which you can access at the following url.


http://127.0.0.1:8000/home

 

This covers the basics of how to add the url and connect it to your methods written in views.py file.

 

Below is the workflow of how we connected the url /home to the django server


 

Django use the MVT framework call the Model, Views, Template 

Views are responsible for handling the requests and responses


3. Use templates

Lets create first Django Template 

create a templates directory inside the homes app and make a welcome.html file inside the another home dir inside the templates as shown below
 
home/templates/home/welcome.html

<html>
<header><title>Raghav!</title></header>
<body><h1>Welcome to the django world</h1></body>
</html>

Now change the response that we were earlier returning from the method home that we created in view.py file
from django.shortcuts import render
from django.http import HttpResponse


# Create your views here.
def home(request):
# return HttpResponse('Hello Raghav')
return render(request, 'home/welcome.html', {})


 

Congrats you have created your first template and now you can run the sevrer again and check the home page 

Comments