$ djang-admin
Type 'django-admin help <subcommand>'forhelp on a specific subcommand.
Available subcommands:
[django]
check
compilemessages
createcachetable
dbshell
diffsettings
dumpdata
flush
inspectdb
loaddata
makemessages
makemigrations
migrate
runfcgi
runserver
shell
showmigrations
sql
sqlall
sqlclear
sqlcustom
sqldropindexes
sqlflush
sqlindexes
sqlmigrate
sqlsequencereset
squashmigrations
startapp
startproject
syncdb
test
testserver
validate
Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
hello django project
$ django-admin.py startproject MyApp; // create a new <MyApp> project
$ cd ./myapp; tree;.
├── db.sqlite3; // default database (MySQL / SQLite 3 / PostgreSQL) settings
├── manage.py; // main program
└── myapp
├── __init__.py; // treat the directory as a package (or a group of modules)
├── settings.py; // Settings/configuration
├── urls.py; // django website urls
└── wsgi.py
1 directory, 5 files
$ python manage.py syncdb; // database synchronization (delete database and recreate tables with new model)
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages
Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Installing custom SQL...
Running migrations:
No migrations to apply.
You have installed Django's auth system, and don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'duho'): duho
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
$ python manage.py migrate; // database migration
$ python manage.py runserver; // or start django web server with the port 8080
$ python manage.py runserver 8090; // or start with the specific port 8090
$ python manage.py runserver 0.0.0.0:8090;
$ wget http://localhost:8090/;
django shell
$ python ./manage.py shell;
simple view test
$ vi ./myapp/views.py;
from django.http import HttpResponse
import datetime
def test(request):
return HttpResponse('Hello, Duho!')
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>Current time is %s.</body></html>" % now
return HttpResponse(html)
def hours_plus(request, offset):
offset = int(offset)
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html = "<html><body>In %s hour(s), it will be %s.</body></html>" %(offset, dt)
return HttpResponse(html)
$ vi ./myapp/urls.py;
urlpatterns = [
url(r'^test/$', 'myapp.views.test', name='test'),
url(r'^time/$', 'myapp.views.current_datetime', name='time'),
url(r'^time/hoursplus/(\d+)/$', 'myapp.views.hours_plus', name='time'),
]
$ python ./manage.py runserver 8090;
$ wget http://localhost:8090/test/;
$ wget http://localhost:8090/time/;
$ wget http://localhost:8090/time/hoursplus/10/;
simple template (html form) test
django shell test
$ python ./manage.py shell;
>>> from django.Template import Template, Context
>>> t = Template("My name is {{ name }}.")
>>> print t
<django.template.Template object at 0xa8c3e35d>
>>> c = Context({"name": "Duho"})
>>> t.render(c)
'My name is Duho.'
>>> person = {'name': 'Duho', 'age': '30'}
>>> person.name
Duho
>>> t2 = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c2 = Context({'person': person})
>>> t2.render(c2)
'Duho is 30 years old.'
>>> class Person(object):
... def __init__(self, first_name, last_name):
... self.first_name, self.last_name = first_name, last_name
>>> t3 = Template('Hello, {{ person.first_name }} {{ person.last_name }}.')
>>> c3 = Context({'person': Person('Duho', 'Kim')})
>>> t3.render(c3)
'Hello, Duho Kim.'
example: Inline Template
$ vi ./myapp/views.py
from django.template import Template, Context
from django.http import HttpResponse
import datetime
def current_datetime_template(request):
now = datetime.datetime.now()
t = Template("<html><body>Current time is {{ current_date }}.</html></body>")
html = t.render(Context({'current_date': now}))
return HttpResponse(html)
$ vi ./myapp/urls.py
urlpatterns = [
url(r'^time_t/$', 'myapp.views.current_datetime_template', name='time'),
]
$ python ./manage.py runserver 8090;
$ wget http://localhost:8090/time_t/;