白手起家搭建django app

 

 

白手起家搭建django app
$django-admin.py startproject web2
$cd web2/
$python manage.py startapp blog
$vim web2/settings.py 
白手起家搭建django app

 

注意settings.py以下两个部分,首先要配置数据库,其次要在INSTALLED_APP里添加刚才新建的app

白手起家搭建django app
DATABASES = { 
    default: {
        ENGINE: django.db.backends.mysql, # Add ‘postgresql_psycopg2‘, ‘mysql‘, ‘sqlite3‘ o
        NAME: dj_db01,                      # Or path to database file if using sqlite3.
        # The following settings are not used with sqlite3:
        USER: root,
        PASSWORD: ‘‘, 
        HOST: localhost,                      # Empty for localhost through domain sockets 
        PORT: ‘‘,                      # Set to empty string for default.
    }   
}
白手起家搭建django app
白手起家搭建django app
INSTALLED_APPS = (
    django.contrib.auth,
    django.contrib.contenttypes,
    django.contrib.sessions,
    django.contrib.sites,
    django.contrib.messages,
    django.contrib.staticfiles,
    blog,
    # Uncomment the next line to enable the admin:
    django.contrib.admin,
    # Uncomment the next line to enable admin documentation:
    django.contrib.admindocs,
)
白手起家搭建django app

接下来编辑models.py

$vim blog/models.py

白手起家搭建django app
from django.db import models

白手起家搭建django app

在mysql里新建对应的数据里

白手起家搭建django app
mysql> create database dj_db01 default charset utf8;
白手起家搭建django app

现在可以自动生成各种表啦

白手起家搭建django app
$ python manage.py syncdb
白手起家搭建django app

现在编辑以下url的config

白手起家搭建django app
$vim web1/urls.py
白手起家搭建django app
白手起家搭建django app
from django.conf.urls import patterns, include, url 

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns(‘‘,
    # Examples:
    # url(r‘^$‘, ‘web1.views.home‘, name=‘home‘),
    # url(r‘^web1/‘, include(‘web1.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^blog/$,blog.views.index),
)
白手起家搭建django app

$mkdir blog/static/images

然后从别的地方拷贝过来一张图,这里我拷贝过来的img2.jpg

接下来编辑一下页面

白手起家搭建django app
$mkdir blog/templates
$vim blog/templates/index.html
白手起家搭建django app

 

白手起家搭建django app
 <h1>hello world</h1>
 <img src=/static/images/img2.jpg/>
白手起家搭建django app

运行一下

白手起家搭建django app
python manage.py runserver 3900
白手起家搭建django app

 

 

 

 

 

 

 

 

 

 

 

 

 

白手起家搭建django app,布布扣,bubuko.com

白手起家搭建django app

上一篇:android中的数据库操作(转)


下一篇:Android开发(一):环境搭建