django-admin.py startproject django_apps
cd django_apps
sudo vim settings.py
DATABASE_ENGINE = 'sqlite3' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'rvdb' # Or path to database file if using sqlite3.
DATABASE_USER = '' # Not used with sqlite3.
DATABASE_PASSWORD = '' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.
python manage.py syncdb
create one now? (yes/no): yes
python manage.py runserver
http://127.0.0.1:8000/
-------------------------------------
python manage.py startapp training
cd training
sudo vim views.py
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
def main_page(request):
template = get_template('main_page.html')
variables = Context({
'head_title': '===Django Training 20090724 ===',
'page_title': 'Welcome to Django Training!',
'page_body': 'Where you can open your eyes!'
})
output = template.render(variables)
return HttpResponse(output)
cd ..
mkdir templates
cd templates
create main_page.html in the templates folder
<html>
<head>
<title>{{ head_title }}</title>
</head>
<body>
<h1>{{ page_title }}</h1>
<p>{{ page_body }}</p>
</body>
</html>
cd ..
sudo vim settings.py
import os.path
TEMPLATE_DIRS = (
os.path.join(os.path.dirname(__file__), 'templates'),
)
sudo vim urls.py
from training.views import *
urlpatterns = patterns('',
(r'^$', main_page),
)
python manage.py runserver
http://127.0.0.1:8000/
-------------------------------------
cd training
sudo vim models.py
from django.db import models
class Computer(models.Model):
name = models.CharField(max_length=20)
ip = models.CharField(max_length=16)
class Employee(models.Model):
name = models.CharField(max_length=20)
mobile = models.CharField(max_length=15)
computers = models.ForeignKey(Computer)
cd ..
sudo vim settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django_apps.training',
)
python manage.py syncdb
python manage.py sql training
BEGIN;
CREATE TABLE "training_computer" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(20) NOT NULL,
"ip" varchar(16) NOT NULL
)
;
CREATE TABLE "training_employee" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(20) NOT NULL,
"mobile" varchar(15) NOT NULL,
"computers_id" integer NOT NULL REFERENCES "training_computer" ("id")
)
;
COMMIT;
sudo vim urls.py
from training.views import *
urlpatterns = patterns('',
(r'^$', main_page),
(r'^employee/(\w+)/$', employee_page),
)
sudo vim view.py
from django.http import HttpResponse, Http404
from training.models import *
def employee_page(request,name1):
try:
employee = Employee.objects.get(name=name1)
except:
raise Http404('Requested employee not found.')
template = get_template('employee_page.html')
variables = Context({
'name': employee.name,
'mobile': employee.mobile,
'computer': employee.computers
})
output = template.render(variables)
return HttpResponse(output)
create employee_page in templates floder.
<html>
<head>
<title>Django Training - Employee: {{ name }}</title>
</head>
<body>
<h1>Training for {{ name }}</h1>
<h2>contact: {{ mobile }}</h2>
{% if computer %}
<ul>
<li>{{ computer.name }} -- {{ computer.ip }}</li>
</ul>
{% else %}
<p>No computers found.</p>
{% endif %}
</body>
</html>
python manage.py runserver
http://127.0.0.1:8000/
-------------------------------------
python manage.py shell
from training.models import *
c=Computer(
name='rvPC',
ip='192.168.225.166'
)
c.save()
e=Employee(
name='han',
mobile='911',
computers=c
)
e.save()
e.computers
python manage.py runserver
http://127.0.0.1:8000/employee/han