标签:return 一个 nis 配置 .com html 工程 环境 技术分享
环境:win7+py3.4+mysql+pymysql
(以下默认工程和应用已经创建)
1.-->工程/settings.py (数据库配置)
DATABASES = { ‘default‘: { ‘ENGINE‘: ‘django.db.backends.mysql‘, ‘HOST‘:‘127.0.0.1‘, ‘PORT‘:3306, ‘NAME‘: ‘bole‘, ‘USER‘:‘root‘, ‘PASSWORD‘:‘root‘, } }
2.-->应用/models.py (数据库字段设置)
from django.db import models class python(models.Model): title = models.CharField(max_length=20) intro = models.CharField(max_length=100) def __str__(self): return self.title
3.-->迁移数据库python manage.py makemigrations + python manage.py migrate
在Mysql数据库中显示
插入测试数据 : python manage.py shell ------->>>from 应用.models import python >>>c = python(title=‘test‘,intro=‘这是个测试‘) >>>c.save() .......
4.-->应用/views.py (编写视图函数显示数据)
from django.shortcuts import render from .models import python def index(request): context = python.objects.all() return render(request,‘index.html‘,context={‘context‘:context})
5.-->应用、工程/urls.py (设置访问路径与视图函数的映射)
应用/urls.py
from django.conf.urls import url from . import views urlpatterns = [ url(r‘^index/$‘,views.index,name = ‘index‘), ]
project/urls.py
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r‘‘,include(‘DB.urls‘)), url(r‘^admin/‘, admin.site.urls), ]
6.-->index.html (编写前端显示模板)
settings.py 中修改
‘DIRS‘: [os.path.join(BASE_DIR,‘templates‘)],
在根目录(manage.py所在目录)新建templates文件夹,在其中新建index.html
<ul> {% for article in context %} <li> 标题:{{article.title}} 内容:{{article.intro}} </li> {% endfor %} </ul>
至此涉及到的M(models)V(views)T(templates)都已完成,python manage.py runserver 在本机运行 http://127.0.0.1:8000/index/
运行另外的py程序修改该数据库内容,刷新后可更新内容
import pymysql try: con = pymysql.connect(host = ‘127.0.0.1‘,user = ‘root‘,passwd=‘root‘,charset=‘utf8‘) con.query(‘create database bole‘) con = pymysql.connect(host = ‘127.0.0.1‘,user = ‘root‘,passwd=‘root‘,db = ‘bole‘,charset=‘utf8‘) except: con = pymysql.connect(host = ‘127.0.0.1‘,user = ‘root‘,passwd=‘root‘,db = ‘bole‘,charset=‘utf8‘) try: pass except: print(‘Table existed‘) print(‘Finished‘) sql = "insert INTO db_python(title,intro) VALUES (‘test4‘,‘这还是一个实时测试‘)" con.query(sql)
-------------------By:羽凡 At:2017-10-6-23:55------------------
标签:return 一个 nis 配置 .com html 工程 环境 技术分享
原文地址:http://www.cnblogs.com/AngelYuFan/p/7633334.html