django云端留言板实例详解

yipeiwu_com5年前Python基础

1.创建应用

django-admin startproject cloudms
cd cloudms
python manage.py startapp msgapp

2.创建模板文件

在cloudms\msgapp\下创建templates文件夹,在templates文件夹下创建MsgSingleWeb.html(这里在pycharm中可以直接选择new一个HTML file,会自动生成html,head,body等标签)内容如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>云端留言板(1)首页</title>
</head>
<body>
  <h1>提交留言功能区</h1>
  <form action="/msggate/" method="post">
    {% csrf_token %}
    发送方 <input type="text" name="userA" /><br>
    接收方 <input type="text" name="userB" /><br>
    消息文 <input type="text" name="msg" /><br>
    <input type="submit" value="留言提交"/>
  </form>

  <h1>获取留言功能区</h1>
  <form action="/msggate/" method="get">
    接收方 <input type="text" name="userC" /><br>
    <input type="submit" value="留言获取">
  </form>
  <table border="1">
    <thead>
      <th>留言时间</th>
      <th>留言来源</th>
      <th>留言信息</th>
    </thead>
    <br>
    <tbody>
      {% for line in data %}
      <tr>
        <td>{{ line.time }}</td>
        <td align="center">{{ line.userA }}</td>
        <td>{{ line.msg }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</body>
</html>

3.引入模板文件
在cloudms\settings.py中修改TEMPLATES=[]中的DIRS,如下

'DIRS': [os.path.join(BASE_DIR,"msgapp/templates")],

4.设定url路由

本地路由。cloudms\msgapp\新建urls.py,内容如下

from django.urls import path
from . import views

urlpatterns=[
  path('',views.msgproc),
]

全局路由引入本地路由,cloudms\cloudms\urls.py内容如下

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
  path("msggate/",include('msgapp.urls')),
  path('admin/', admin.site.urls),
]

5.编写views的交互函数

cloudms\msgapp\views.py内容如下

from django.shortcuts import render
from datetime import datetime
# Create your views here.
def msgproc(request):
  datalist=[]
  if(request.method=="POST"):
    userA=request.POST.get("userA",None)
    userB=request.POST.get("userB",None)
    msg=request.POST.get("msg",None)
    time=datetime.now()
    with open('msgdata.txt','a+') as f:
      f.write("{}--{}--{}--{}--\n".format(userB,userA,msg,time.strftime("%Y-%m-%d %H:%M:%S")))

  if(request.method=="GET"):
    userC=request.GET.get("userC",None)
    if(userc!=None):
      with open('msgdata.txt','r') as f:
        cnt=0
        for line in f:
          linedata=line.split('--')
          if(linedata[0]==userC):
            d={"userA":linedata[1],"msg":linedata[2],"time":linedata[3]}
            datalist.append(d)
          if(cnt>=10):
            break
  return render(request,"MsgSingleWeb.html",{"data":datalist}) ##render函数第三个参数是字典类型,表明向html页面中特定变量赋值

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】

Python实现正弦信号的时域波形和频谱图示例【基于matplotlib】

本文实例讲述了Python实现正弦信号的时域波形和频谱图。分享给大家供大家参考,具体如下: # -*- coding: utf-8 -*- # 正弦信号的时域波形与频谱图 impor...

Python numpy中矩阵的基本用法汇总

Python numpy中矩阵的基本用法汇总

Python矩阵的基本用法 mat()函数将目标数据的类型转化成矩阵(matrix) 1,mat()函数和array()函数的区别 Numpy函数库中存在两种不同的数据类型(矩阵ma...

python 利用jinja2模板生成html代码实例

这篇文章主要介绍了python 利用jinja2模板生成html代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 from...

在Python中使用Neo4j数据库的教程

在Python中使用Neo4j数据库的教程

 一个快速的REST例子 首先来看些基本知识。如果没有服务API,Neo4j就不能支持其他语言。该接口提供一组基于JSON消息格式的RESTful Web服务和一个全面的发现机...

Python实现的旋转数组功能算法示例

本文实例讲述了Python实现的旋转数组功能算法。分享给大家供大家参考,具体如下: 一、题目 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 例1: 输入: [...