安装django
sudo
pip
install
django==1.8.3
实用虚拟环境
source venv/bin/activate
deactivate
创建django 项目
django-admin startproject tutorial
cd
tutorial
python manage.py startapp blog
python manage.py migrate
python manage.py runserver
配置models
class
Article(models.Model):
title
=
models.CharField(u
"博客标题"
,max_length
=
100
)
#博客标题
category
=
models.CharField(u
"博客标签"
,max_length
=
50
,blank
=
True
)
#博客标签
pub_date
=
models.DateTimeField(u
"发布日期"
,auto_now_add
=
True
,editable
=
True
)
#博客发布日期
update_time
=
models.DateTimeField(u
‘更新时间‘
,auto_now
=
True
,null
=
True
)
content
=
models.TextField(blank
=
True
, null
=
True
)
# 博客文章正文
def
__unicode__(
self
):
return
self
.title
class
Meta:
#按时间下降排序
ordering
=
[
‘-pub_date‘
]
verbose_name
=
"文章"
verbose_name_plural
=
"文章"
同步数据库
python manage.py makemigrations
创建超级用户
python manage.py createsuperuser
配置urls
新建blog.urls
tutorial.urls
import blog.urls as blog_url
urlpatterns
=
[
url(r
‘^admin/‘
, include(admin.site.urls)),
url(r
‘^ueditor/‘
,include(djud_urls)),
]
blog.urls
from django.conf.urls import url
from . import views
urlpatterns = [
url(r‘^test/‘, views.Test, name="blog_test"),
url(r‘^post/(?P<id>\d+)/$‘,views.Detail,name="blog_detail"),
url(r‘^home/‘,views.home,name="blog_home"),
]
from . import views
urlpatterns = [
url(r‘^test/‘, views.Test, name="blog_test"),
url(r‘^post/(?P<id>\d+)/$‘,views.Detail,name="blog_detail"),
url(r‘^home/‘,views.home,name="blog_home"),
]
setting配置,配置HTML文件
‘DIRS‘: [os.path.join(BASE_DIR,"templates")],templates文件和managy.py同级