1.Install 

sudo apt-get install python-pip
sudo pip install django==1.8

 2. Create Project

django-admin startproject projname

3. Create App

cd prjname
python manage.py startapp appname

4. Center URL&&setting

Control center in projname/projname/
##url config
Url Pattern setting
url(r^$,include('tcmwebapp.urls')),
url(r'^tcmwebapp',include('tcmwebapp.urls')),
## static url setting
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

## setting config
INSTALL APP( appname )

5. app urls&&view,

## url config
cd appname/
touch urls.py
url(r'xxx',views.fucname,name=XXX)
### views.py
def fucname():
return HttpResponse("helloworld")
cd projname/
python manage.py runserver
(tip:0.0.0.0:)
chrome localhost:8090
### helloworld

6. Global Config

cd projname/
mkdir static
mkdir templates
cd appname/
mkdir static/appname
mkdir templates/appname

7. static&templates&setting AutoTool: djangobower

7.1 Install.

sudo apt-get install python-software-properties
sudo apt-get install node
sudo apt-get install nodejs
sudo apt-get install npm
sudo apt-get install git
curl -SL http://deb
.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install nodejs-legacy
sudo npm install -g bower
sudo pip install django-bower

7.2 Config

http://django-bower.readthedocs.io/en/latest/index.html
import os PROJECT_ROOT = os.path.abspath(
os.path.join(os.path.dirname(__file__), ".."),
) DEBUG = True
TEMPLATE_DEBUG = DEBUG DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'db',
'USER': '',
'PASSWORD': '',
'HOST': '',
'PORT': '',
}
} STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static') STATIC_URL = '/static/' BOWER_COMPONENTS_ROOT = os.path.join(PROJECT_ROOT, 'components') STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
) SECRET_KEY = 'g^i##va1ewa5d-rw-mevzvx2^udt63@!xu$-&di^19t)5rbm!5' TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
) MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
) ROOT_URLCONF = 'example.urls' WSGI_APPLICATION = 'example.wsgi.application' TEMPLATE_DIRS = (
os.path.join(PROJECT_ROOT, 'templates'),
) INSTALLED_APPS = (
'django.contrib.staticfiles',
'djangobower',
) BOWER_INSTALLED_APPS = (
'jquery',
'underscore',
)

7.2 Use

python manage.py bower_install
python manage.py collectstatic
Change projname center UrlConfig
urlpattern += (settings.STATIC_URL,name=settings.STATIC_Document)
urlpattern +=(settings.MEDIA_URL,name=settings.MEDIA_Document)

8. Design Web Templates

8.1 View distrubution

python manage.py makemigrations
python manage.py migrate
### update database

8.2 create html 

8.2.1 base.html

projectname/templates/base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE =edge">
<meta name="viewport" content = "width=device-width,initial-scale=1">
<title>
{% block title %}WebNet{% endblock %}
</title>
{% load staticfiles %}
<link rel="stylesheet" href={% static 'bootstrap/dist/css/bootstrap.css' %}>
<script src={% static 'jquery/dist/jquery.min.js' %}></script>
<script src={% static 'bootstrap/dist/js/bootstrap.min.js' %}></script>
{% block head %}
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
<div class="scripts">
{% block scripts %}
{% endblock %}
</div>
</body>
</html>

8.2.3 user_base.html

{% extends "base.html" %}
{% load staticfiles %}
{% block head %}
<!-- <link rel="stylesheet" href={% static 'usercenter/css/style.css' %}>
--> {% endblock %}
{% block scripts %}
{% endblock %}

8.3.4 web html

{% extends "netapp/base_netapp.html" %}
{% block body %}
<link href="/static/netapp/css/search.css" rel="stylesheet"> <section class="content">
<div class="container-fluid">
<div class="form">
<div class="form-title">心脏毒性网络</div>
<div class="form-body">
<form class="navbar-form" action="/netapp/search" method="post" role="search"> {% csrf_token %}
<table>
<input type="text" class="form-control" name="chem_name" size="60" maxlength="60" placeholder="Chemical Name">
<button type="submit" class="btn btn-danger navbar-btn">Submit</button>
</table>
</form>
</div>
</div>
</div>
</section>
{% endblock %}
{% block scripts %}
<!-- <script src = "/static/usercenter/js/userApp.js"></script> -->
{% endblock %}

8.3.5 css add

.form{
margin-top: 150px;
margin-right: 10px;
margin-left: 10px;
text-align: center;
} .form-title{
font-weight:bold;
font-family:arial,verdana,sans-serif;
font-size:18px;
}

9. Design View

from django.shortcuts import render
from django.http import HttpResponse
from tasks import chem_gene_query
# Create your views here.
def index(request):
return render(request,"netapp/index.html") def search(request):
if request.method == "POST":
message = request.POST["chem_name"]
result = chem_gene_query(message)
return render(request,"netapp/result.html",{"result":result})
return render(request,"netapp/index.html")

10. add scripts in project 

#!/usr/bin/env/python
# -*- coding:UTF-8 -*-
from __future__ import print_function
import MySQLdb
import sys def mysql_connect():
try:
conn = MySQLdb.connect(host="localhost",user="tcd_net",
passwd='tcd_net',db='ctd_net',port=3306,charset='utf8')
cur = conn.cursor()
return conn,cur
except MySQLdb.Error,e:
print(e.args)
sys.exit(1) def query_db(conn,cursor,command):
try:
cursor.execute(command)
conn.commit()
results = cursor.fetchall()
return results
except MySQLdb.Error,e:
print(e.args)
sys.exit(1) def chem_gene_query(chem):
conn,cur = mysql_connect()
query_command = """select chemicalName,chemicalID,casId,
genesymbol,geneid,geneforms,organism,organismid,interaction,
interactionActions,pubmedid from chem_gene where chemicalName = "%s";"""%chem
chem_gene_result = query_db(conn,cur,query_command)
result = []
keys = ["chemicalName","chemicalID","casId","genesymbol",
"geneid","geneforms","organism","organismid",
"interaction","interactionActions","pubmedid"]
for res in chem_gene_result:
out = {}
for i in range(len(res)):
out[keys[i]] = res[i]
if out:
result.append(out)
return result

11. Design results Display

{% extends "netapp/base_netapp.html" %}
{% block body %}
<link href="/static/netapp/css/search.css" rel="stylesheet"> <section class="content">
<div class="container-fluid">
<table class="table table-bordered table-condensed">
<caption>SearchResults</caption>
<thead>
<tr>
<th>chemicalName</th>
<th>genesymbol</th>
<th>interactionActions</th>
<th>pubmedid</th>
</tr>
</thead>
{% for i in result %}
<tbody>
<tr>
<td>{{ i.chemicalName }}</td>
<td>{{ i.genesymbol }}</td>
<td>{{ i.interactionActions }}</td>
<td>{{ i.pubmedid }}</td>
</tr>
</tbody>
{% endfor %}
</table>
</div>
</section>
{% endblock %}
{% block scripts %}
<!-- <script src = "/static/usercenter/js/userApp.js"></script> -->
{% endblock %}

python 培训之Django的更多相关文章

  1. 2015老男孩Python培训第八期视频教程

    2015老男孩Python培训第八期视频教程,希望您通过本教程的学习,能学会常用方法和技巧.教程从基础知识开始讲解一直到后期的案例实战,完全零基础学习,从初学者的角度探讨分析问题,循序渐进由易到难,确 ...

  2. 老王Python培训视频教程(价值500元)【基础进阶项目篇 – 完整版】

    老王Python培训视频教程(价值500元)[基础进阶项目篇 – 完整版] 教学大纲python基础篇1-25课时1.虚拟机安装ubuntu开发环境,第一个程序:hello python! (配置开发 ...

  3. 曾Python培训讲师-2年Python开发无包装简历-20191217-可公开

    目录 个人介绍 技能介绍 项目经历 自我评价 简历非完整版,需要完整版看下述信息,禁止任何一切私人用途.转发 我生日是27号,那就27元一份,有需求的来购买!只会涨价不会降价,大概卖10份涨1元:曾P ...

  4. Python攻关之Django(一)

    课程简介: Django流程介绍 Django url Django view Django models Django template Django form Django admin (后台数据 ...

  5. python框架之django

    python框架之django 本节内容 web框架 mvc和mtv模式 django流程和命令 django URL django views django temple django models ...

  6. Python Virtualenv运行Django环境配置

    系统: RHEL6.5 版本说明: Python-3.5.0 Django-1.10.4 virtualenv:为每个项目建立不同的/独立的Python环境,你将为每个项目安装所有需要的软件包到它们各 ...

  7. 【Python实战】Django建站笔记

    前一段时间,用Django搭建一个报表分析的网站:借此正好整理一下笔记. 1. 安装 python有包管理工具pip,直接cd Python27/Scripts,输入 pip install djan ...

  8. 智普教育Python培训之Python开发视频教程网络爬虫实战项目

    网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 01.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Python视频 02.mp4 网络爬虫项目实训:看我如何下载韩寒博客文章Pytho ...

  9. python学习笔记--Django入门四 管理站点--二

    接上一节  python学习笔记--Django入门四 管理站点 设置字段可选 编辑Book模块在email字段上加上blank=True,指定email字段为可选,代码如下: class Autho ...

随机推荐

  1. XML的解析和保存

    1.XML(extensible markup language;XML )  定义:,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.     XML语法规范:  标 ...

  2. 查询和修改(Queries and Mutations)

    On this page, you'll learn in detail about how to query a GraphQL server. 在这个页面,你将会学习更多的关于如何查询GraphQ ...

  3. 【日常笔记】datatables表格数据渲染

    现在有很多表格渲染方式 这里只是记录怎么使用datatables渲染数据 使用datatables可以更方便的来渲染数据 [中文api]http://datatables.club/index.htm ...

  4. jsp内置对象作业3-application用户注册

    1,注册页面 zhuCe.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...

  5. iOS开发小技巧--巧用ImageView中的mode(解决图片被拉伸的情况)

    一.自己遇到的问题:在布局ImageView的时候,通过约束将ImageView布局好,但是里面的图片被拉伸的很难看.这时候就用到了Mode属性,如图: 代码实现方式: 二.让图片按照比例拉伸,并不是 ...

  6. Shell脚本_判断apache是否启动

      安装nmap:  yum install nmap -y nmap 127.0.0.1   脚本 vim apache_is_start.sh chmod 755 apache_is_start. ...

  7. 【CodeVS 1218】【NOIP 2012】疫情控制

    http://codevs.cn/problem/1218/ 比较显然的倍增,但是对于跨过根需要很多讨论,总体思路是贪心. 写了一上午,不想再说什么了 #include<cstdio> # ...

  8. ansible解密

    ansible是个什么东西呢?官方的title是“Ansible is Simple IT Automation”——简单的自动化IT工具.这个工具的目标有这么几项:让我们自动化部署APP:自动化管理 ...

  9. poj3233 矩阵等比数列求和 二分

    对于数列S(n) = a + a^2 + a^3 +....+ a^n; 可以用二分的思想进行下列的优化. if(n & 1) S(n) = a + a^2 + a^3 + ....... + ...

  10. jquery-通过js编写弹出窗口

    本文转载 本文主要是通过js动态控制div的高度,css控制浮动 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...