Django高级实战 开发企业级问答网站 学习 教程
Django高级实战 开发企业级问答网站
1. 创建项目与app
创建项目
django-admin startproject firstsite
创建app
python manage.py startapp firstapp
2.settings里面添加app
编辑 firstsite/settings.py
:
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'firstapp', # 添加刚才创建的app
)
3. 编辑models层
打开 firstapp/models.py
,编辑如下内容:
from django.db import models # 创建一个叫 Article 的类,它继承自django.db.models.Model, # headline 和 content 是它的属性,都继承自django.db.models.Field # 并定义了属性的数据类型和限制 class Article(models.Model): headline = models.CharField(null=True, blank=True, max_length=200) content = models.TextField(null=True, blank=True) # 让实例在后台管理界面显示自己的headline def __str__(self): return self.headline
4. 创建和合并数据库
对 Model 做了修改后,使用 makemigrations
命令,你的 Model 会被扫描, 然后与 migrations
文件夹中以前的版本作比较, 然后生成本次迁移文件。
python manage.py makemigrations
有了新的 migration
文件,就可以使用 migrate
命令修改数据库模式:
python manage.py migrate
每次修改完 Model 后都应该执行上诉命令。
5.设置模板路径
在 firstapp 文件夹内创建 templates
和 static
两个文件夹,html 文件放入 templates
文件夹,css、js 和图片等文件放入 static
中.
文档目录树如下:
然后在 settings.py
中修改模板路径:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 模板路径,意思是:找到根目录,在根目录下添加 templates
'DIRS': [os.path.join(BASE_DIR, 'firstapp', 'templates').replace('\\','/')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
6.创建管理后台和超级管理员
在 firstsite 目录下打开命令行,输入:
python manage.py createsuperuser
按要求,填上用户名、邮箱、密码,打开,登录管理界面,就能看到以下界面:
要让 Article 出现在管理界面,需要在 admin.py
引入相应的数据项:
from django.contrib import admin
from firstapp.models import Article # c从模板层引入Article
admin.site.register(Article) # 在后台管理中添加 Article
这样后台就多了 Article 一项了。
你可以点进去添加新内容:
7.在 View 中获取 Model 的数据
编辑 views.py
,引入 model 中文章列表,然后渲染网页:
from django.shortcuts import render from firstapp.models import Article # 从modles引入刚创建的类Article from django.template import Template,Context def index(request): # 全部的文章列表 article_list = Article.objects.all() # 一个字典结构,包装要渲染的数据 context = {'article_list': article_list} # 渲染,包含三个参数:request、模板 html 的名字、context index_page = render(request, 'first_web.html', context) return index_page
8.在 Template 中增加动态内容
编辑 templates
文件夹里相应的 html 文件(这里我命名为:first_web.html
):
<!DOCTYPE html>
<!-- Django 的静态文件标签 -->
{% load staticfiles %}
<html>
<head>
<meta charset="utf-8">
<title>first_web</title>
<!-- 引入 static 文件夹里面的css样式 -->
<link rel="stylesheet" href="{% static 'css/semantic.css' %}" media="screen" title="no title">
</head>
<body>
<div class="ui segment">
<h1 class="ui centered header">Your Blog</h1>
</div>
<div class="ui basic segment">
<!-- 引入 static 文件夹里面的图片 -->

</div>
<!-- 用一个 for 循环来顺序展示 article 里的内容 -->
{% for article in article_list %}
<div class="ui very padded segment">
<h2>
<i class="star icon