Full Stack Web Developer BootCamp. (A course by Jose Portilla on Udemy) - Updated for recent versions of Python and Django
1.
Install Django using Python pip feature.
Command prompt: pip install django
2.
Create your project under your working
directory. Let us call it "first_project".
Command prompt: django-admin startproject first_project
This command will create the following files under the first_project
directory - asgi.py, settings.py,
urls,py and wsgi.py:
i. __init__.py is
empty, and lets python know that this directory can be treated as a package.
ii. url.py store all
urls for the project.
iii. wsgi.py acts as
the web server gateway interface. it helps deploy on Production.
iv. manage.py -
Associated with many commands as we build our web app.
3.
Change directory to first_project and run the
server to host it on localhost/port 8000.
Command prompt: manage.py runserver.
Once you have confirmed that the site opens without any issues, close
the server by pressing Ctrl + C.
4.
Create your Application with the name ‘first_app’.
Command Prompt: manage.py startapp first_app
This creates the first_app folder inside first_project, and has the following files:
i.
Migrations (folder) – Contains database
specific information as it relates to the models.
ii.
__init__.py - Lets python know that this
directory can be treated as a package.
iii.
admin.py - register your models here, which
django will us with its admin interface.
iv.
apps.py - place application specific
configurations
v.
models.py - store application's data models.
Entities and Relationships between data
vi.
tests.py - series of functions to test code
vii.
views.py - series of functions that handle
requests and responses.
5.
Register your application with your project.
i. Open first_project/settings.py.
ii Scroll down to
[INSTALLED_APPS] section.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'first_app'
]
Include first_app in installed Apps in the settings.py in the first_project folder as shown above.
6.
Create a view and map it the url.
i.
Open the first_app/views.py file and paste the
code below for a very simple view.
from django.shortcuts import
render
from django.http import
HttpResponse
# Create your views here.
def
index(request):
return HttpResponse("Hello World")
ii.
Open the first_project/urls.py file and include
this view in the router.
from django.contrib import
admin
from django.urls import
path
from
first_app import views
urlpatterns = [
path('',
views.index, name='index'),
path('admin/', admin.site.urls),
]
7.
Start the server
Command Prompt: manage.py runserver
You should now see a screen like this.
8.
Separate Application and Project URL Mapping
for modularity
i. Create a new file - ...first_app/urls.py
from
django.urls
import
path
from
first_app import
views
urlpatterns = [
path('', views.index, name='index'),
]
ii. Tell the first_project/url.py to use first_app url for the application. To do this, include the two lines in first_project/url.py
from django.contrib import adminfrom django.urls import pathfrom first_app import viewsfrom django.conf.urls import include
urlpatterns = [
path('', views.index, name='index'),path('first_app/', include('first_app.urls')),path('admin/', admin.site.urls),]
9.
Test functionality for both - the home page and the first_app
page.
Command Prompt: manage.py runserver
a. The home page on http://127.0.0.1:8000
should show Hello World (as routed from first_project/urls.py)
b. The page on http://127.0.0.1:8000/first_app should also show hello world, but this time, as routed from the first_app/urls.py


Comments
Post a Comment