Internet & Web Application

Django Note


Django : Python WebApp Framework

– Download & Install Django by
> python setup.py install

Basic run-able Django WebApp

– Create project directory
> django-admin.py startproject <project name>

– Go into <project name> directory
> cd <project name>

  • __init__.py: An empty file that tells Python that this directory should be considered a Python package. (Read more about packages in the official Python docs if you're a Python beginner.)
  • manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin.py and manage.py.
  • settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.
  • urls.py: The URL declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs in URL dispatcher.

– Test server, 1st run-time
> python manage.py runserver 0.0.0.0:8000

Database system initialization

– Database setup , in settings.py
see information here ..

*** if you’re using ‘MySQL’ database engine, please create ‘database name’ as you’ve set in ‘settings.py’.

– To make database complete with ‘database table’ that be need by ‘django app’ ..
> python manager.py syncdb

The result is =>

MVC model in Django

– Model in Django’s called => ‘Model’

– View in Django’s called => ‘Template’

– Controller in Django’s called => ‘View’

To ignore confussion, you should say that django is ‘MTV model’.

Create models (database interface)

– create app
> python manage.py startapp polls

… A project is a collection of configuration and apps for a particular Web site …

– First thing to do, is to define ‘Model’ .. look in polls/models.py .. edit them to

… The goal is to define your data model in one place and automatically derive things from it.

– to activating models .. tell our project , ‘polls’ , is installed .. edit settings.py

– then ..
> python manage.py sql polls
SQL command for tables creation ‘re automatically generated …

If you’re interested, also run the following commands:

  • python manage.py validate — Checks for any errors in the construction of your models.
  • python manage.py sqlcustom polls — Outputs any custom SQL statements (such as table modifications or constraints) that are defined for the application.
  • python manage.py sqlclear polls — Outputs the necessary DROP TABLE statements for this app, according to which tables already exist in your database (if any).
  • python manage.py sqlindexes polls — Outputs the CREATE INDEX statements for this app.
  • python manage.py sqlall polls — A combination of all the SQL from the sql, sqlcustom, and sqlindexes commands.

– make tables
> python manage.py syncdb

Read the django-admin.py documentation for full information on what the manage.py utility can do.

Advertisements
มาตรฐาน

ใส่ความเห็น

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out / เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out / เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out / เปลี่ยนแปลง )

Connecting to %s