------------------------------------------------------------ 总结 ------------------------------------------------------------
1. 安装python和django并配置环境变量;
2. 将 文件复制到目录A(A表示你想在该目录下创建项目);
3. 输入【 python startproject project_name 】创建项目;
4. 输入【 python .\ runserver 127.0.0.1:8000 】运行本地IP,跑起项目,此时即可选择浏览器,输入网址 “http://127.0.0.1:8000/” 打开网页;
5. 输入【 python .\ startapp appName 】创建app项目,并进行如下配置:
1)手动添加;
2)配置【】,包括appName、templates、static等(后续的数据库类型也是在这里配置);
3)理解每个appName下的和projectName目录下的的关系,比进行配置;
4)编写appName目录下的【】,返回变量和具体网页;
6. 输入【 python .\ sycndb 】创建数据库
-------------------------------------------------------------------------------------------------------------------------------
,python,django下载安装好后,新建一个项目:mysite (左侧栏中的django)
文件说明如下:
- 【】一个实用的命令行工具,可让你以各种方式与该 Django 项目进行交互。
- 【__init__.py】一个空文件,告诉 Python 该目录是一个 Python 包。
- 【】该 Django 项目的设置/配置。
- 【】该 Django 项目的 URL 声明; 一份由 Django 驱动的网站"目录"。
- 【】一个 WSGI 兼容的 Web 服务器的入口,以便运行你的项目。
-------------------------------------------------------------------------------------------------------------------------
a.创建一个Django项目,并且使用 python startapp blog 命令创建一个APP,并且在中添加blog
INSTALLED_APPS = (
'',
'',
'',
'',
'',
'',
'blog',
)
b.数据库的操作:
首先,在MySQL里面创建一个叫做 blog 的数据库;然后在中加入数据库配置如下:
DATABASES = {
'default': {
'ENGINE': '',
'NAME': 'blog',
'USER': 'name',
'PASSWORD':'pass',
'HOST':'localhost',
'PORT':'3306',
}
}
c.在中放入如下代码作为数据库的描述:
# encoding:utf8
from import models
from import admin
class Article():
title = (max_length=100) # 博客题目
category = (max_length=50, blank=True) # 博客标签
date_time = (auto_now_add=True) # 博客日期
content = (blank=True, null = True) # 博客文章正文
def __unicode__(self):
return
class Meta: # 按时间下降排序
ordering = ['-date_time']
# Register model
(Article)
d.同步数据库
python migrate
python makemigrations
python migrate
e.是界面以及相应的数据库操作:
1、在中的TEMPLATES的DIRS位置修改如下:
'DIRS': [BASE_DIR+"/templates",],
2、在TEMPLATE目录下添加HTML文件,代码如下:
(1)
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="A layout example that shows off a blog page with a list of posts.">
<title>Vincent's Blog</title>
<link rel="stylesheet" href="/pure/0.5.0/">
<link rel="stylesheet" href="/pure/0.5.0/">
<link rel="stylesheet" href="/">
</head>
<body>
<div class="pure-g">
<div class="sidebar pure-u-1 pure-u-md-1-4">
<div class="header">
<h1 class="brand-title">Vincent's Blog</h1><br>
<h2 class="brand-tagline">不想变成咸鱼的猫</h2><br>
<nav class="nav">
<ul class="nav-list">
<li class="nav-item">
<a class="pure-button" href="/MrJoeyM">Github</a>
</li>
<li class="nav-item">
<a class="pure-button" href="/github_39611196">****</a>
</li>
</ul>
</nav>
</div>
</div>
<div class="content pure-u-1 pure-u-md-3-4">
<div>
{% block content %}
{% endblock %}
<div class="footer">
<div class="pure-menu pure-menu-horizontal pure-menu-open">
<ul>
<li><a href="/MrJoeyM">Github</a></li>
<li><a href="/github_39611196">****</a></li>
</ul>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
(2).
{% extends "" %}
{% block content %}
<div class="posts">
{% for post in post_list %}
<section class="post">
<header class="post-header">
<h2 class="post-title">{{ }}</h2>
<p class="post-meta">
Time: <a class="post-author" href="#">{{ post.date_time }}</a> <a class="post-category post-category-js" href="#">{{ }}</a>
</p>
</header>
<div class="post-description">
<p>
{{ }}
</p>
</div>
</section>
{% endfor %}
</div><!-- /.blog-post -->
{% endblock %}
(3)、在中添加如下代码:
from import Article
from datetime import datetime
from import HttpResponse
from import render
# Create your views here.
def home(request):
post_list = ()
return render(request, '', {'post_list':post_list})
def detail(request, my_args):
post = ()[int(my_args)]
str = ("title = %s, category = %s, date_time = %s, content = %s"
% (, , post.date_time, ))
return HttpResponse(str)
(4)、修改为如下所示:
from import include, url
from import admin
urlpatterns = [
path('admin/', ),
path('index/',home),
]
启动项目,在浏览器中输入 localhost:8000
创建后台账号:
python createsuperuser
http://127.0.0.1:8000/admin/