Django学习系列4:url templates配置

提取mysql内的数据,显示到html中:

编辑views.py

# _*_ coding: utf-8 _*_
from django.shortcuts import render
import MySQLdb

from .models import UserMessage

# Create your views here.

def getform(request):
#all_messages = UserMessage.objects.all()
all_messages = UserMessage.objects.filter(name = 'zone1')
if all_messages:
message = all_messages[0]

# if request.method == "POST":
# name = request.POST.get('name','')
# message = request.POST.get('message','')
# address = request.POST.get('address','')
# email = request.POST.get('email','')
# user_message = UserMessage()
# user_message.name = name
# user_message.message = message
# user_message.address = address
# user_message.email = email
# user_message.object_id= "helloworld4"
#
# user_message.save()

return render(request, 'message_form.html',{
"my_message":message
})

说明:

# UserMessage.objects.filter(name = 'zone1')方法取出所有那么为zone1的记录
all_messages = UserMessage.objects.filter(name = 'zone1')
if all_messages:
#将记录分片,第一条赋值给message
message = all_messages[0]

return render(request, 'message_form.html',{
"my_message":message #将message信息传递给变量my_message
})

html页面里传递my_message: value=”{{ my_message.name }}

html页面如下:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="/static/css/style.css">
</head>
<body>
<form action="{% url 'go_from' %}" method="post" class="smart-green">
<h1>留言信息
<span>请留下你的信息.</span>
</h1>
<label>
<span>姓名 :</span>
<input id="name" type="text" value="{{ my_message.name }}"name="name" class="error" placeholder="请输入您的姓名"/>
<div class="error-msg"></div>
</label>

<label>
<span>邮箱 :</span>
<input id="email" type="email" value="{{ my_message.email }}" name="email" placeholder="请输入邮箱地址"/>
<div class="error-msg"></div>
</label>

<label>
<span>联系地址 :</span>
<input id="address" type="text" value="{{ my_message.address }}" name="address" placeholder="请输入联系地址"/>
<div class="error-msg"></div>
</label>

<label>
<span>留言 :</span>
<textarea id="message" value="{{ my_message.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>

前端页面已经取出值:

templates语法if:如果name是zone1则展示出zonetest

<input id="name" type="text" value="{% if  my_message.name == 'zone1' %}zonetest
{% else %}zone not test{% endif %}"name="name" class="error" placeholder="请输入您的姓名"/>

templates语法ifequal:

<input id="name" type="text" value="{% ifequal  my_message.name 'zone1' %}zonetest
{% else %}zone not test{% endifequal %}"name="name" class="error" placeholder="请输入您的姓名"/>

templates语法slice: 切片

<input id="name" type="text" value="{% ifequal  my_message.name|slice:'4' 'zone1' %}zonetest{% else %}zone not test{% endifequal %}" name="name" class="error" placeholder="请输入您的姓名"/>

因为slice4,所以message.name被切片为zone而非zone1,所以页面展示出zone not test:

templates方法有多少种?   google搜索django document

英文地址:https://docs.djangoproject.com/en/2.1/

汉化地址:https://docs.djangoproject.com/zh-hans/2.0/

url配置技巧:url可以配置一个名字。编辑url页面,设置name=‘go_from’

urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^form/$', getform, name='go_from')
]

编辑html页面:

<form action="{% url 'go_from' %}" method="post" class="smart-green">

这样的好处是,设置了别名,变更url时就不涉及到变更templates里的url。

发表评论

邮箱地址不会被公开。