X Tutup
Skip to content

Latest commit

 

History

History
106 lines (84 loc) · 2.51 KB

File metadata and controls

106 lines (84 loc) · 2.51 KB

Notes

Django Commands Usage

Create a Project

django-admin startproject <projectname>

If you are in the directory django, run

django-admin startproject mysite

will create the following files:

django/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

Here,

  • manage.py: command-line utility that lets you interact with this Django project in various ways.
  • mysite/: actual Python package for your project.
  • mysite/__init__.py: empty file that makes mysite/ a Python package.
  • mysite/settings.py: settings/configuration for this Django project.
  • mysite/urls.py: the URL declarations for this Django project; a "table of contents" of your Django-powered site.
  • mysite/wsgi.py: an entry-point for WSGI-compatible web servers to serve your project.

Start the Development Server

python manage.py runserver

If you want to change the server's port, pass it as a command-line argument, e.g.:

python manage.py runserver 8080

Create an App

In the directory that contains manage.py, run

python manage.py startapp <appname>

where appname is the name of the application you want to add. For example,

python manage.py startapp logs

will create a directory logs in django with the following layout:

logs/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    views.py

Create a Superuser

python manage.py createsuperuser

You may need to supply an username, an email, and a password.

Migrate the Models

python manage.py makemigrations [<app_name>]

creates new migrations based on the changes detected to your models.

Synchronize the Database with Models

python manage.py migrate

synchronizes the database state with the current set of models and migrations.

Django Basics

Points:

  • Steps of creating web pages with Django:
    1. define a URL for the page to be created;
    2. write a view funtion to retrieve and process the data needed for the page;
    3. write a template to build the page for the browser.
  • Fields are specified by class attributes. They are the only required part of a model defining the database fields.
  • Built-in field types: TODO:
  • Any page that lets a user enter and submit information on a web page is a form.

Misc

  • Superuser name: DeepWalter
  • Password: have fun
X Tutup