{{ post.title }}
{{ post.content }}
------------------------------------------------------------ 总结 ------------------------------------------------------------
1. 安装python和django并配置环境变量;
2. 将 django-admin.py 文件复制到目录A(A表示你想在该目录下创建项目);
3. 输入【 python django-admin.py startproject project_name 】创建项目;
4. 输入【 python .\manage.py runserver 127.0.0.1:8000 】运行本地IP,跑起项目,此时即可选择浏览器,输入网址 “http://127.0.0.1:8000/” 打开网页;
5. 输入【 python .\manage.py startapp appName 】创建app项目,并进行如下配置:
1)手动添加urls.py;
2)配置【setting.py】,包括appName、templates、static等(后续的数据库类型也是在这里配置);
3)理解每个appName下的urls.py和projectName目录下的urls.py的关系,比进行配置;
4)编写appName目录下的【views.py】,返回变量和具体网页;
6. 输入【 python .\manage.py sycndb 】创建数据库
-------------------------------------------------------------------------------------------------------------------------------
1.pycharm,python,django下载安装好后,新建一个项目:mysite (左侧栏中的django)
文件说明如下:
-------------------------------------------------------------------------------------------------------------------------
a.创建一个Django项目,并且使用 python manage.py startapp blog 命令创建一个APP,并且在settings.py中添加blog
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)
b.数据库的操作:
首先,在MySQL里面创建一个叫做 blog 的数据库;然后在settings.py中加入数据库配置如下:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER': 'name',
'PASSWORD':'pass',
'HOST':'localhost',
'PORT':'3306',
}
}
c.在models.py中放入如下代码作为数据库的描述:
# encoding:utf8
from django.db import models
from django.contrib import admin
class Article(models.Model):
title = models.CharField(max_length=100) # 博客题目
category = models.TextField(max_length=50, blank=True) # 博客标签
date_time = models.DateTimeField(auto_now_add=True) # 博客日期
content = models.TextField(blank=True, null = True) # 博客文章正文
def __unicode__(self):
return self.title
class Meta: # 按时间下降排序
ordering = ['-date_time']
# Register model
admin.site.register(Article)
d.同步数据库
python manage.py migrate
python manage.py makemigrations
python manage.py migrate
e.是界面以及相应的数据库操作:
1、在settings.py中的TEMPLATES的DIRS位置修改如下:
'DIRS': [BASE_DIR+"/templates",],
2、在TEMPLATE目录下添加HTML文件,代码如下:
(1)base.html
Vincent's Blog
{% block content %}
{% endblock %}
(2).home.html
{% extends "base.html" %}
{% block content %}
{% for post in post_list %}
{{ post.title }}
{{ post.content }}
{% endfor %}
{% endblock %}
(3)、在views.py中添加如下代码:
from blog.models import Article
from datetime import datetime
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home(request):
post_list = Article.objects.all()
return render(request, 'home.html', {'post_list':post_list})
def detail(request, my_args):
post = Article.objects.all()[int(my_args)]
str = ("title = %s, category = %s, date_time = %s, content = %s"
% (post.title, post.category, post.date_time, post.content))
return HttpResponse(str)
(4)、修改urls.py为如下所示:
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
path('admin/', admin.site.urls),
path('index/',home),
]
启动项目,在浏览器中输入 localhost:8000
创建后台账号:
python manage.py createsuperuser
http://127.0.0.1:8000/admin/