2011年5月4日 星期三

first template with views

1.modify settings.py to add templates folder on project
#mkdir templates
#vim settings.py
import os.path
TEMPLATE_DIRS = (
os.path.join( os.path.dirname( __file__ ), 'templates' ),
)

2.add new main_page.html on templates
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{{ head_title }}</title>
</head>

<body>
<h1>{{ page_title }}</h1>
<p>{{ page_body }}</p>
</body>
</html>

3.modify 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 Bookmarks',
'page_title':'Welcome to use Django frameworkd',
'page_body':'Where you can store and share bookmarks',
} )
output = template.render( variables )
return HttpResponse( output )

create Bookmark model to connect with user

1.add Bookmark model on models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Link( models.Model ):
url = models.URLField( unique = True )

class Bookmark( models.Model ):
title = models.CharField( max_length = 200 )
user = models.ForeignKey( User )
link = models.ForeignKey( Link )

2.sync db with python manage.py
#python manage syncdb

create first model

1.modify models.py to add new model link

from django.db import models
# Create your models here.
class Link( models.Model ):
url = models.URLField( unique = True )

2.use sql syntax to see create table
#python manage sql bookmarks

3.create table
#python manage syncdb

4.use shell to create records
#python manage shell
from bookmarks.model import *
link1 = Link(url="http://www.google.com.tw")
link1.save()
link2 = Link(url="http://tw.yahoo.com")
link2.save

2011年5月3日 星期二

create first main_page

1.modify urls.py add new line
urlpatterns = patterns( '',
# Examples:
# url(r'^$', 'django_bookmarks.views.home', name='home'),
# url(r'^django_bookmarks/', include('django_bookmarks.foo.urls')),

# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

# Uncomment the next line to enable the admin:
url( r'^admin/', include( admin.site.urls ) ),
url( r'^$', main_page ),
)

2.add new content on views.py

from django.http import HttpResponse
def main_page( request ):
output = '''
<html>
<head><title>%s</title></head>
<body>
<h1>%s</h1><p>%s</p>
</body>
</html>''' % ( 'Django Bookmarks', 'welcome to use django', 'nice to meet you' )
return HttpResponse( output )

add admin interface on django

1.uncomment the admin line on settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)

2.uncomment the lines on urls.py
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
# Uncomment the next line to enable the admin:
url( r'^admin/', include( admin.site.urls ) ),

3.use syncdb to create tables on bookmarks
#python manage syncdb

first new project

1.create new project name django_bookmarks
#django-admin.py startproject django_bookmarks

2.modify setting to connect mysql
#vim settings

ADMINS = (
# ('ssnoodles', 'ssnoodles@gmail.com'),
)

MANAGERS = ADMINS

DATABASES = {
'default': {
'ENGINE': 'mysql',
'NAME': 'bookmarks',
'USER': 'root',
'PASSWORD': 'abc123',
'HOST': 'localhost',
'PORT': '3306',
}
}
TIME_ZONE = 'Asia/Taipei'

3.create my first application bookmarks
#python manage startapp bookmarks

4.create bookmarks db on mysql server
#mysql -u root -pabc123
create database bookmarks

5.to check django web framework is ok or no?
#python manage runserver 192.168.11.102:9000

install django develop environment

1.install ubuntu os
2.install mysql-server
#apt-get install mysql-server mysql-client

3.install ssh
#apt-get install ssh

4.install proftpd
#apt-get install proftpd

5.install vim
#apt-get install vim

6.modify proftpd.conf and add rootlogin
#vim /etc/proftpd/proftpd.conf
==================================
RootLogin on
==================================

7.restart proftpd service
#/etc/init.d/proftpd restart

8.install python-dev package for develop tools
#apt-get install python-dev

9.install python-MySQLdb module
#apt-get install python-MySQLdb

10.modify my.cf to allow access mysql from anywhere
#vim /etc/mysql/my.cnf
comment the below line
#bind-address = 127.0.0.1

11.restart mysql service
#/etc/init.d/mysql restart