djanjo实战环境搭建
- 虚拟环境搭建
具体内容详见环境搭建
## env 仓库
pip install virtualenvwrapper-win
- mysql 和 pycharm使用自行学习
django简单使用
- django基础知识
# 创建app
tool 工具中 run manage.py task
startapp appname
- django基本目录结构
apps 项目工程
media 用户上传文件
log 日志文件
static 静态文件
- mysql 数据库连接以及默认表生成
下载对应驱动
跳转虚拟环境
pip install MySQL_python-1.2.5-cp27-none-win32.whl
更改数据库驱动
setting.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'edu_online',
'USER': 'root',
'PASSWORD': '123456',
'HOST': '127.0.0.1'
}
}
django 基本表文件生成
makemigrations
migrate
- django运行原理
apps中 views 返回
def getform(request):
return render(request,'message-form.html')
url 连接映射
utls.py
from message.views import getform
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^form/', getform)
]
模板+静态文件地址设置
setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'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',
],
},
},
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
- django model 的创建
- 创建model
# __*__ coding:utf-8 __*__
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class UserMessage(models.Model):
object_id = models.CharField(primary_key=True,max_length=50,verbose_name=u'主键',default="")
name = models.CharField(max_length=64,verbose_name=u'名称')
email = models.EmailField(verbose_name=u'邮箱')
word = models.CharField(max_length=512,verbose_name=u'留言')
class Meta:
verbose_name= u'用户留言'
app注册
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'message'
]
表结构文件生成
makemigrations
migrate
model 实现增删改查
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<form action="/form/" method="post" class="smart-green">
<h1>留言信息
<span>请留下你的信息.</span>
</h1>
<label>
<span>姓名 :</span>
<input id="name" type="text" name="name" class="error" placeholder="请输入您的姓名"/>
<div class="error-msg"></div>
</label>
<label>
<span>邮箱 :</span>
<input id="email" type="email" value="" name="email" placeholder="请输入邮箱地址"/>
<div class="error-msg"></div>
</label>
<label>
<span>联系地址 :</span>
<input id="address" type="text" value="" name="address" placeholder="请输入联系地址"/>
<div class="error-msg"></div>
</label>
<label>
<span>留言 :</span>
<textarea id="message" name="message" placeholder="请输入你的建议"></textarea>
<div class="error-msg"></div>
</label>
<div class="success-msg"></div>
<label>
<span> </span>
<input type="submit" class="button" value="提交"/>
</label>
{% csrf_token %}
</form>
</body>
</html>
.smart-green {
margin-left: auto;
margin-right: auto;
max-width: 500px;
background: #F8F8F8;
padding: 30px 30px 20px 30px;
font: 12px Arial, Helvetica, sans-serif;
color: #666;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
}
.smart-green h1 {
font: 24px "Trebuchet MS", Arial, Helvetica, sans-serif;
padding: 20px 0px 20px 40px;
display: block;
margin: -30px -30px 10px -30px;
color: #FFF;
background: #9DC45F;
text-shadow: 1px 1px 1px #949494;
border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
-moz-border-radius: 5px 5px 0px 0px;
border-bottom: 1px solid #89AF4C;
}
.smart-green h1 > span {
display: block;
font-size: 11px;
color: #FFF;
}
.smart-green label {
display: block;
margin: 0px 0px 5px;
}
.smart-green label > span {
float: left;
margin-top: 10px;
color: #5E5E5E;
}
.smart-green input[type="text"], .smart-green input[type="email"], .smart-green textarea, .smart-green select {
color: #555;
height: 30px;
line-height: 15px;
width: 100%;
padding: 0px 0px 0px 10px;
margin-top: 2px;
border: 1px solid #E5E5E5;
background: #FBFBFB;
outline: 0;
-webkit-box-shadow: inset 1px 1px 2px rgba(238, 238, 238, 0.2);
box-shadow: inset 1px 1px 2px rgba(238, 238, 238, 0.2);
font: normal 14px/14px Arial, Helvetica, sans-serif;
}
.smart-green textarea {
height: 100px;
padding-top: 10px;
}
.smart-green .button {
background-color: #9DC45F;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-border-radius: 5px;
border: none;
padding: 10px 25px 10px 25px;
color: #FFF;
text-shadow: 1px 1px 1px #949494;
}
.smart-green .button:hover {
background-color: #80A24A;
}
.error-msg {
color: red;
margin-top: 10px;
}
.success-msg {
color: #80A24A;
margin-top: 10px;
margin-bottom: 10px;
}
# -*- coding:utf-8 -*-
from django.shortcuts import render
from .models import UserMessage
# Create your views here.
def getform(request):
# 查询所有
all_messages = UserMessage.objects.all()
# 删除所有
all_messages.delete()
for message in all_messages:
name = message.name
# 查询单个
all_messages = UserMessage.objects.filter(name='xiaojinzi',address='anhui')
for message in all_messages:
# 删除单个
message.delete()
name = message.name
# 修改
if name == 'xiaojinzi':
message.address = '安徽'
message.save()
if request.method == "POST":
name = request.POST.get("name")
email = request.POST.get("email")
message = request.POST.get("message")
address = request.POST.get("address")
user_message = UserMessage()
user_message.name = name
user_message.email = email
user_message.message = message
user_message.address = address
user_message.object_id = 'xiaojinziya'
user_message.save()
return render(request,'message-form.html')
- django templete 应用
模板语言具体模板语言规范 django document
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<!--url函数 维护性强-->
<form action="{% url 'message_form' %}" method="post" class="smart-green">
<h1>留言信息
<span>请留下你的信息.</span>
</h1>
<label>
<span>姓名 :</span>
<!--后台传值接受-->
<input id="name" type="text" name="name" class="error" value="{{ my_message.name }}" placeholder="请输入您的姓名"/>
<div class="error-msg"></div>
</label>
<label>
<span>邮箱 :</span>
<input id="email" type="email" value="" name="email" placeholder="请输入邮箱地址"/>
<div class="error-msg"></div>
</label>
<label>
<span>联系地址 :</span>
<input id="address" type="text" value="" name="address" placeholder="请输入联系地址"/>
<div class="error-msg"></div>
</label>
<label>
<span>留言 :</span>
<textarea id="message" name="message" placeholder="请输入你的建议"></textarea>
<div class="error-msg"></div>
</label>
<div class="success-msg"></div>
<label>
<span> </span>
<input type="submit" class="button" value="提交"/>
</label>
{% csrf_token %}
</form>
</body>
</html>
urls
url(r'^form/', getform,name='message_form')
views
return render(request,'message-form.html',{
'my_message':message
})