17 Jun 2020

Django Concepts

  • django
  • concepts
  • python
  • todolist
  • webapp
  • Helloworld Web App github

    Make Todo List

    python manage.py COMMAND allows you to interact with django project

    Create a new app

    $python manage.py startapp {APP_NAME}

    Settings.py

    When creating a new app within the project, the app’s name has to be added to INSTALLED_APPS

    urls.py

    Import new functions from {app_name}.views import {FUNCTION} and add path('{URL}', {FUNCTION})

    models.py

    class MODEL_ITEM(models.Model):
        DATA_FIELD = models.TextField()
        DATA_FIELD = models.CharField(max_length = 29)
        ...
    
    $python manage.py makemigrations
    $python manage.py migrate

    views.py

    If you created a separate html for the app in templates directory, add from django.shortcuts import render django.shortcuts is a package that collects helper functions and classes that span multiple levels of MVC and introudce control coupling.

    Control coupling is one module controlling the flow of another by passing it information on what to do.

    reference

    render(request, template_name, context=None, content_type=None, using=None)

    render is gathering data, loading the associated templates, applying the data to the templates, and sending the output to the user.

    from django.http import HttpResponse, HttpResposneRedirect

    HttpRequest objects that contain metadata are created automatically by Django

    Request and response objects are used to pass state through the system.

    Page is requested -> HttpRequest created with metadata about the request -> load view with HttpRequest -> each returns HttpResponse object

    return HttpResponse('{url}' or {"{msg}"})

    from .models import MODEL_ITEM imports model item from models.py. MODEL_ITEM(DATA_FIELD = request.METHOD['INPUT_NAME'])

    settings.py

    import os

    Add os.path.join(BASE_DIR, 'templates') in TEMPLATES > DIRS

    In your live server environment, tell WSGI app what settings file to use

    WSGI is the Web Server Gateway Interface that forwards requests to web apps or frameworks in Python

    OS module provides functions for interacting with the operating system. os.path.join({a file system path},{path component to be joined}) BASE_DIR is an absolute path where manage.py lies

    templates > .html

    </pre>