python Django搭建部落格
部署環境
- 系統CentOS Linux release 7.4.1708
- python為Python 3.5
- Django為Django-2.1.4
升級python2.7到python3.5 參考文章
- 下載python3.5 wget -S https://www.python.org/ftp/python/3.5.6/Python-3.5.6.tgz
- yum install openssl-devel
- yum install zlib
- yum install sqlite-devel(django依賴,不安裝後面需要安裝)
- tar -zxvf Python-3.5.6.tgz
- cd Python-3.5.6
- ./configure
- make&&make install
- mv /usr/bin/python /usr/bin/python.old1
- cp /usr/local/bin/python3 /usr/local/bin/python3.bak
- ln -s /usr/local/bin/python3 /usr/bin/python
- vim /usr/bin/yum 把#!/usr/bin/python替換為#!/usr/bin/python.old
安裝Django
- cp /usr/bin/pip /usr/bin/pip.old
- cp /usr/local/bin/pip3 /usr/bin/pip
- pip install --upgrade pip
- /usr/local/python3/bin/pip3 install django
建立django專案並開始一個新的app
- mkdir /usr/local/src/gaoliang
- cd /usr/local/src/gaoliang
- django-admin startproject bolog
- python manage.py migrate
- python manage.py runserver 預設為127.0.0.1:8000埠,遠端不能訪問,需要遠端訪問 manage.py
- 同時還需將你的IP將入到settings.py中
ALLOWED_HOSTS = ['你的ip']
或者 ALLOWED_HOSTS = ['*'] 上面完成後初步搭建完成; http://47.110.153.193:8000/
建立使用者
- python manage.py createsuperuser
user:root passwd:123456qwer - python manage.py runserver 0.0.0.0:8000
- 登入地址:http://ip:8000/admin/
但是現在頁面不是那麼好看,而且介面是英文的,那麼我們需要做一些修改
首先我們先安裝一個bootstrap的外掛
- 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', ###app名稱 )
LANGUAGE_CODE = 'zh-hans'
TIME_ZONE = 'Asia/Shanghai'
修改後的,首先插入bootstrap的管理頁面外掛,然後語言設定為中文,時區更改為中國。
編輯admin.py
from django.contrib import admin
from blog.models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title','pub_date')
admin.site.register(Article,ArticleAdmin)
為你的部落格建立個模型
開啟blod/models.py
#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 dbshell
delete from django_migrations where app='bolog';
rm -rf migrations/ pycache/
python manage.py makemigrations
python manage.py migrate