白手起家搭建django app

$

$django-admin.py startproject web2

$cd web2/

$python manage.py startapp blog

$vim web2/settings.py

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

DATABASES = {

‘default’: {

‘ENGINE’: ‘django.db.backends.mysql’,

‘NAME’: ‘dj_db01’,

‘USER’: ‘root’,

‘PASSWORD’: ”,

‘HOST’: ‘localhost’,

‘PORT’: ”,

}

}

INSTALLED_APPS = (

‘django.contrib.auth’,

‘django.contrib.contenttypes’,

‘django.contrib.sessions’,

‘django.contrib.sites’,

‘django.contrib.messages’,

‘django.contrib.staticfiles’,

‘blog’,

‘django.contrib.admin’,

‘django.contrib.admindocs’,

)

接下来编辑models.py

$vim blog/models.py

from django.db import models

在mysql里新建对应的数据库

mysql> create database dj_db01 default charset utf8;

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

$python manage.py syncdb

$vim web2/urls.py

from django.conf.urls import patterns, include, url

from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns(”,

url(r’^blog/$’, ‘blog.view.index’),

)

$mkdir blog/static/images

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

现在来编辑一下页面

$mkdir blog/templates

$vim blog/templates/index.html

<h1>hello</h1>

<img src=’/static/images/img2.jpg’/>

运行一下:

$python manage.py runserver 3900

 

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注