码迷,mamicode.com
首页 > 移动开发 > 详细

白手起家搭建django app

时间:2014-06-08 22:18:14      阅读:377      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

 

 

bubuko.com,布布扣
$django-admin.py startproject web2
$cd web2/
$python manage.py startapp blog
$vim web2/settings.py 
bubuko.com,布布扣

 

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

bubuko.com,布布扣
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.
    }   
}
bubuko.com,布布扣
bubuko.com,布布扣
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,
)
bubuko.com,布布扣

接下来编辑models.py

$vim blog/models.py

bubuko.com,布布扣
from django.db import models

bubuko.com,布布扣

在mysql里新建对应的数据里

bubuko.com,布布扣
mysql> create database dj_db01 default charset utf8;
bubuko.com,布布扣

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

bubuko.com,布布扣
$ python manage.py syncdb
bubuko.com,布布扣

现在编辑以下url的config

bubuko.com,布布扣
$vim web1/urls.py
bubuko.com,布布扣
bubuko.com,布布扣
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),
)
bubuko.com,布布扣

$mkdir blog/static/images

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

接下来编辑一下页面

bubuko.com,布布扣
$mkdir blog/templates
$vim blog/templates/index.html
bubuko.com,布布扣

 

bubuko.com,布布扣
 <h1>hello world</h1>
 <img src=/static/images/img2.jpg/>
bubuko.com,布布扣

运行一下

bubuko.com,布布扣
python manage.py runserver 3900
bubuko.com,布布扣

 

 

 

 

 

 

 

 

 

 

 

 

 

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

白手起家搭建django app

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/iwangzheng/p/3775509.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!