DJango: building website

 1. Create / Start a new web project

  1. cmd: django-admin startproject project_name
  2. cmd: cd project_name
  3. cmd: python manage.py startapp application_name
  4. under application_name folder, create a new file urls.py
  5. open views.py under application_name folder, create a view:
    from django.shortcuts import render

    def example_view(request):
        return render(request, 'my_app/example.html')
        #refer to my_app/templates/my_app/example.html
  6. in the urls.py under application_name, connect to view to the url:
    from django.urls import path
    from . import views

    urlpatterns=[
        path('', views.example_view, name = "example_page")
    ]
  7. in the project_name folder, open urls.py, import "include", and include the application_name urls:
    from django.contrib import admin
    from django.urls import path, include

    urlpatterns = [
        path('admin/', admin.site.urls),
        path('my_app/', include('my_app.urls')),
    ]
  8. at project level, cmd: python manage.py migrate
  9. at project level, go to settings.py, in the session of INSTALLED_APPS, add the AppConfig (check the name in apps.py in the application folder). don't forget the comma
    INSTALLED_APPS = [
        'my_app.apps.MyAppConfig',
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
    ]
  10. at project level, cmd: python manage.py makemigrations application_name. At later stage, after making changes, cmd: python manage.py migrate
  11. at application level, create a new folder called "templates". in the "templates" folder, create a folder called application_name, and put html files in it.
  12. at project level, cmd: python manage.py runserver