- The method is basically learned and modifies from Corey Schafer’s Youtube Django tutorial: https://www.youtube.com/watch?v=UmljXZIypDc&list=PL-osiE80TeTtoQCKZ03TU5fNfx2UY6U4p
- Specific codes in my project: https://www.yuque.com/akiencore/qgdgg5/wy1v2u#aHjoq
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
[root@localhost Python]# django-admin startproject django_project# django-admin startproject [your desired project name]# now we have[root@localhost Python]# tree.└── django_project├── django_project│ ├── __init__.py│ ├── settings.py│ ├── urls.py│ └── wsgi.py└── 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
[root@localhost Python]# cd django_project/[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
- in the example blog/ we have:- db.sqlite3: the database of the project after you create the first app.- urls.py: same as urls.py before, direct urls to the correct application.- views.py: what urls.py finally directs to, includes JavaScript code or template variables.- to make sure all apps are correctly used, sometimes we need to add new routes or new pages for new apps.- For example, /blog/urls.py:```pythonfrom django.urls import pathfrom . import views # import from current directoryurlpatterns = [path('', views.home, name='blog-home'), # def homepath('about/', views.about, name='blog-about'), # def about]
we have 2 routes, one for the homepage, one for an “about” page.
and they are finally related to functions in /blog/views.py:
from django.shortcuts import renderfrom django.http import HttpResponsedef home(request):return HttpResponse('<h1>Blog Home</h1>')# newdef about(request):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’
- paste in INSTALLED_APPS in django_project/settings.py:```python//...INSTALLED_APPS = ['blog.apps.BlogConfig', # path name + function name, divided by dots'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles',]//...
create 2 directories to save templates:
[root@localhost django_project]# mkdir blog/templates[root@localhost django_project]# mkdir blog/templates/blog
For example, make a HTML page for home called home.html in blog/templates/blog/
<!DOCTYPE html><html><head><title></title></head><body><h1>Blog Home!</h1></body></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’)
#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:
- get into the specific application page for paths in [project_name]/urls.py
- use the specific part of the application for paths in [app_name]/urls.py
- functions in [app_name]/views.py handle those requests
- 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.
