{{ post.title }}
{{ post.content|safe }}
sudo pip install django==1.8.3
django-admin startproject tutorial
cd tutorial
python manage.py startapp blog
数据迁移
python manage.py migrate
python manage.py runserver
访问: http://127.0.0.1:8000
启动成功。
#coding:utf8
from __future__ import unicode_literals
from django.db import 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 migrate
创建超级用户:
python manage.py createsuperuser
分别有user password email 用户自定义填写。
在blog:admin 中注册
#coding:utf8
from django.contrib import admin
from blog.models import Article
# Register your models here.
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title','pub_date')
admin.site.register(Article,ArticleAdmin)
运行项目:
python manage.py runserver
首先我们先安装一个bootstrap的插件
(sudo) pip install bootstrap-admin
然后在tutorial/settings.py中更改一些代码
INSTALLED_APPS = (
'bootstrap_admin',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
和
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
此时登录admin界面,界面转化为中文字样。
https://github.com/twz915/DjangoUeditor3
可以下载zip或者直接clone,将里面的DjangoUeditor直接放到本项目的根目录
ls # 第二行为显示的为此文件包中的内容
blog db.sqlite3 DjangoUeditor manage.py README.md templates tutorial
并且在settings中加入此插件应用。
INSTALLED_APPS = (
...
'blog',
'DjangoUeditor',
)
在tutorial:url中
from django.conf.urls import include, url
from django.contrib import admin
from DjangoUeditor import urls as djud_urls
from django.conf import settings
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^ueditor/',include(djud_urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
上方这是为了让django能访问编辑器模块
然后在tutorial/settings.py中加入
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'static')
#公共的static文件
STATICFILES_DIRS = (
os.path.join(BASE_DIR,"common_static"),
os.path.join(BASE_DIR,"media"),
)
#upload floder
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR,'media')
STATICFILES_FINDERS = ("django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",)
其中:
STATIC_URL 这个是放置静态文件的地方,
django会默认在这个文件夹中寻找需要的静态文件,
很多教程教大家上来就把静态文件塞这里面其实这并不是一个好的处理方法,
因为在发布前需要统一收集静态文件的时候会从各个文件夹中收集静态文件放入这个文件夹中,
期间有可能会覆盖掉原来的文件。
STATIC_ROOT 这个就是静态文件相对于系统的目录
MEDIA_URL 一般会将上传的文件放入这个文件夹
MEDIA_ROOT 同STATIC_ROOT
STATICFILES_DIRS 这一个元组,里面放置开发时静态文件自动搜寻的目录,
我们在开发是先建一个common_static即公用的静态文件夹,
在里面放我们自己的静态文件,等最后使用静态文件收集命令一并处理。
然后我们更改blog/models.py 中的模型
from DjangoUeditor.models import UEditorField
content = UEditorField(u"文章正文",height=300,width=1000,default=u'',blank=True,imagePath="uploads/blog/images/",
toolbars='besttome',filePath='uploads/blog/files/')
迁移数据库:
python manage.py makemigrations
python manage.py migrate
我们在blog/views.py中加入:
def Test(request):
return HttpResponse("just a test")
在blog中添加urls.py
#coding:utf8
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^test/',views.Test,name="blog_test"),
]
在tutorial的urls中引入blog的urls:
import blog.urls as blog_url
urlpatterns = [
url(r'^blog/',include(blog_url)),
url(r'^admin/', include(admin.site.urls)),
url(r'^ueditor/',include(djud_urls)),
]
运行后可以正常访问,返回just a test。
我们尝试着通过视图views访问数据库,更改/blog/views.py:
from blog.models import Article
def Test(request):
post = Article.objects.all()
return HttpResponse(post[0].content)
如果你刚在后台添加了文章那么就能看到它了。
在项目目录下有一个名叫templates的文件夹,如果没有你就建一个。
新建一个html文件templates/blog/test.html:
Just test template
Hello World!
{{ current_time }}
修改blog:views.py :
def Test(request) :
return render(request, 'blog/test.html',
{'current_time': datetime.now()})
新建3个html放入templates、blog中:
base.html:
李琼羽的博客
{% block content %}
{% endblock %}
home.html
{% extends "blog/base.html" %}
{% block content %}
{% for post in post_list %}
{{ post.title }}
{{ post.content|safe }}
Read More >>>
{% endfor %}
{% endblock %}
post.html
{% extends "blog/base.html" %}
{% block content %}
{% for post in post_list %}
{{ post.title }}
{{ post.content|safe }}
Read More >>>
{% endfor %}
{% endblock %}
其中,home.html和post.html继承于base.html
更改blog/views.py:
#coding:utf8
from django.shortcuts import render
from django.http import HttpResponse
from blog.models import Article
from datetime import datetime
from django.http import Http404
# Create your views here.
def home(request):
post_list = Article.objects.all() # 获取全部的Article对象
return render(request, 'blog/home.html', {'post_list': post_list})
def Test(request):
return render(request,'blog/test.html',{'current_time': datetime.now()})
def Detail(request,id):
try:
post = Article.objects.get(id=str(id))
except Article.DoesNotExist:
raise Http404
return render(request,'blog/post.html',{'post':post})
blog/urls.py
#coding:utf8
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^post/(?P\d+)/$',views.Detail,name="blog_detail"),
url(r'^home/',views.home,name="blog_home"),
url(r'^test/',views.Test,name="blog_test"),
]
tutorial/urls.py
from django.conf.urls import include, url
from django.contrib import admin
import blog.urls as blog_url
from DjangoUeditor import urls as djud_urls
from django.conf import settings
urlpatterns = [
url(r'^blog/',include(blog_url)),
url(r'^admin/', include(admin.site.urls)),
url(r'^ueditor/',include(djud_urls)),
]
if settings.DEBUG:
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL,document_root = settings.MEDIA_ROOT)
运行访问http://127.0.0.1:8000/blog/home/
详细步骤见http://www.cnblogs.com/Liqiongyu/articles/5893780.html