Purpose:

  • Create a server-client program (is similar to Facebook or Twitter)
  • Composed of several separate but interactive applications, such as accounts, blog posts, etc.

Environment settings

  • Requirements
    • Programming Language: Python
    • Database: sqlite3
    • Operating System: Linux
    • Software Framework: Django

Step 1: Create the project

  1. [root@localhost Python]# django-admin startproject django_project
  2. # django-admin startproject [your desired project name]
  3. # now we have
  4. [root@localhost Python]# tree
  5. .
  6. └── django_project
  7. ├── django_project
  8. ├── __init__.py
  9. ├── settings.py
  10. ├── urls.py
  11. └── wsgi.py
  12. └── manage.py
  • setting.py: includes setting variable of the project, such as INSTALLED_APPS, TEMPLATES.
  • urls.py: inlcudes a pattern that directs different url paths into different applications.
  • manage.py: the key program that we need to run the server every time.

Step 2: To run the server

  1. [root@localhost Python]# cd django_project/
  2. [root@localhost django_project]# python manage.py runserver
  • Use the url in “Starting development server at (url)” to view the page.

Step 3: Add apps

  • After the tree is built, now spread branches: ```bash [root@localhost django_project]# python manage.py startapp blog

now we have

[root@localhost djangoproject]# tree . ├── blog │ ├── admin.py │ ├── apps.py │ ├── init.py │ ├── migrations │ │ └── init.py │ ├── models.py │ ├── tests.py │ └── views.py ├── db.sqlite3 ├── djangoproject │ ├── init.py │ ├── pycache │ │ ├── __init.cpython-36.pyc │ │ ├── settings.cpython-36.pyc │ │ ├── urls.cpython-36.pyc │ │ └── wsgi.cpython-36.pyc │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py

4 directories, 17 files

  1. - in the example blog/ we have:
  2. - db.sqlite3: the database of the project after you create the first app.
  3. - urls.py: same as urls.py before, direct urls to the correct application.
  4. - views.py: what urls.py finally directs to, includes JavaScript code or template variables.
  5. - to make sure all apps are correctly used, sometimes we need to add new routes or new pages for new apps.
  6. - For example, /blog/urls.py:
  7. ```python
  8. from django.urls import path
  9. from . import views # import from current directory
  10. urlpatterns = [
  11. path('', views.home, name='blog-home'), # def home
  12. path('about/', views.about, name='blog-about'), # def about
  13. ]

we have 2 routes, one for the homepage, one for an “about” page.
and they are finally related to functions in /blog/views.py:

  1. from django.shortcuts import render
  2. from django.http import HttpResponse
  3. def home(request):
  4. return HttpResponse('<h1>Blog Home</h1>')
  5. # new
  6. def about(request):
  7. return HttpResponse('<h1>Blog About</h1>')

Step 4: Apply templates

  • Grab the function name in apps.py in the current app folder: ```python from django.apps import AppConfig

class BlogConfig(AppConfig): name = ‘blog’

  1. - paste in INSTALLED_APPS in django_project/settings.py:
  2. ```python
  3. //...
  4. INSTALLED_APPS = [
  5. 'blog.apps.BlogConfig', # path name + function name, divided by dots
  6. 'django.contrib.admin',
  7. 'django.contrib.auth',
  8. 'django.contrib.contenttypes',
  9. 'django.contrib.sessions',
  10. 'django.contrib.messages',
  11. 'django.contrib.staticfiles',
  12. ]
  13. //...
  • create 2 directories to save templates:

    1. [root@localhost django_project]# mkdir blog/templates
    2. [root@localhost django_project]# mkdir blog/templates/blog
  • For example, make a HTML page for home called home.html in blog/templates/blog/

    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <title></title>
    5. </head>
    6. <body>
    7. <h1>Blog Home!</h1>
    8. </body>
    9. </html>
  • change blog/views.py to redirect the request to that HTML page, and a template is used: ```python from django.shortcuts import render from django.http import HttpResponse

handle the traffic from homepage to blog

def home(request): return render(request, ‘blog/home.html’)

  1. #return HttpResponse('<h1>Blog Home</h1>')

def about(request): return HttpResponse(‘

Blog About

‘) ```

  • Using templates can help us reuse codes.

Step 5: Modify files for corresponding apps

  • The sequence of clients to use an application:
  1. get into the specific application page for paths in [project_name]/urls.py
  2. use the specific part of the application for paths in [app_name]/urls.py
  3. functions in [app_name]/views.py handle those requests
  4. those functions are shown to clients as webpages in [app_name]/templates/ [app_name]/[method].html
  • According to 4 steps, modify your codes for different requirements.