由于大部分资料(比如《Django Web开发指南》)都是python2.x的
踩了很多坑,特此更新macOS环境下学习Django 建立Blog方法
1、安装Django
[>>>pip3 install django
Successfully installed django-2.0.7 pytz-2018.5
2、在目标文件夹终端创建自己的blog项目
[>>>django-admin.py startproject <projectname>
比如:
[>>>django-admin.py startproject abcBlog
3、校验本地服务器环境
[>>>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
浏览器打开http://127.0.0.1:8000/ 查看debug环境 ctrl+C退出开始安装blogAPP
4、安装blog APP
[>>>python3 manage.py startapp blog
5、Setting文件配置
image
很多材料写<projectname>.blog,这种写法在django-2.0.7以上会报错
AttributeError: module <projectname> has no attribute 'blog'
数据库缺省
image.png
6、创建库表配置
models.py文件添加配置
from django.db import models
# Create your models here.
# 博客类由标题、主体、时间组成,分别使用char、Text、DateTime三种不同控件
class BlogsPost(models.Model):
title = models.CharField(max_length=150)
body = models.TextField()
timestamp = models.DateTimeField()
image.png
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)
image.png
7、执行数据库同步创建表
[>>>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、创建超级用户
[>>>python3 manage.py createsuperuser
Username (leave blank to use 'xxx'):
Email address:
Password:
Password (again):
Superuser created successfully.
[>>>python3 manage.py runserver
浏览器打开http://127.0.0.1:8000/admin/
登录(刚才创建的账户密码)
登录界面
尝试发帖,这里用到的就是models.py里配置的BlogsPost
查看管理界面,这里用到的就是admin.py里配置的BlogsPostAdmin
网友评论