由于大部分資料(比如《Django Web開(kāi)發(fā)指南》)都是python2.x的
踩了很多坑换途,特此更新macOS環(huán)境下學(xué)習(xí)Django 建立Blog方法
1娱据、安裝Django
[>>>pip3 install django
Successfully installed django-2.0.7 pytz-2018.5
2胶果、在目標(biāo)文件夾終端創(chuàng)建自己的blog項(xiàng)目
[>>>django-admin.py startproject <projectname>
比如:
[>>>django-admin.py startproject abcBlog
3荒适、校驗(yàn)本地服務(wù)器環(huán)境
[>>>python3 manage.py runserver
July 02, 2018 - 09:48:51
Django version 2.0.7, using settings 'abcBlog.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[02/Jul/2018 09:49:02] "GET / HTTP/1.1" 200 16348
[02/Jul/2018 09:49:02] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
[02/Jul/2018 09:49:02] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
[02/Jul/2018 09:49:02] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
[02/Jul/2018 09:49:02] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
Not Found: /favicon.ico
[02/Jul/2018 09:49:02] "GET /favicon.ico HTTP/1.1" 404 1979
瀏覽器打開(kāi)http://127.0.0.1:8000/ 查看debug環(huán)境 ctrl+C退出開(kāi)始安裝blogAPP
4怒见、安裝blog APP
[>>>python3 manage.py startapp blog
5丁寄、Setting文件配置
很多材料寫(xiě)<projectname>.blog页滚,這種寫(xiě)法在django-2.0.7以上會(huì)報(bào)錯(cuò)
AttributeError: module <projectname> has no attribute 'blog'
數(shù)據(jù)庫(kù)缺省
6崔步、創(chuàng)建庫(kù)表配置
models.py文件添加配置
from django.db import models
# Create your models here.
# 博客類(lèi)由標(biāo)題穿肄、主體年局、時(shí)間組成,分別使用char咸产、Text矢否、DateTime三種不同控件
class BlogsPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
admin.py文件添加管理界面配置
from django.contrib import admin
from blog.models import BlogsPost
# Register your models here.
class BlogsPostAdmin(admin.ModelAdmin):
list_display = ('title', 'body', 'timestamp')
admin.site.register(BlogsPost, BlogsPostAdmin)
7、執(zhí)行數(shù)據(jù)庫(kù)同步創(chuàng)建表
[>>>python3 manage.py makemigrations blog
Migrations for 'blog':
blog/migrations/0001_initial.py
- Create model BlogsPost
[>>>python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, blog, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying auth.0009_alter_user_last_name_max_length... OK
Applying sessions.0001_initial... OK
Applying blog.0001_initial... OK
8脑溢、創(chuàng)建超級(jí)用戶(hù)
[>>>python3 manage.py createsuperuser
Username (leave blank to use 'xxx'):
Email address:
Password:
Password (again):
Superuser created successfully.
[>>>python3 manage.py runserver
瀏覽器打開(kāi)http://127.0.0.1:8000/admin/
登錄(剛才創(chuàng)建的賬戶(hù)密碼)