django云端留言板实例详解

作者:roadwide 时间:2023-12-13 08:17:59 

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页面 * 定变量赋值

来源:https://www.cnblogs.com/roadwide/p/11143360.html

标签:django,云端,留言板
0
投稿

猜你喜欢

  • 对Tensorflow中权值和feature map的可视化详解

    2021-03-31 22:24:39
  • Python在centos7.6上安装python3.9的详细教程(默认python版本为2.7.5)

    2022-05-09 18:58:43
  • python设计模式之抽象工厂模式详解

    2023-06-11 22:15:51
  • python使用pika库调用rabbitmq交换机模式详解

    2024-01-01 06:28:18
  • Python3 字典dictionary入门基础附实例

    2023-03-08 18:57:49
  • SQLServer触发器创建、删除、修改、查看示例代码

    2024-01-22 16:33:03
  • python实现大转盘抽奖效果

    2023-12-16 13:50:30
  • Python实战使用Selenium爬取网页数据

    2021-06-18 19:11:56
  • python logging通过json文件配置的步骤

    2022-06-04 22:30:19
  • python实现给微信指定好友定时发送消息

    2023-04-27 04:55:35
  • JDBC连接Sql Server 2005总结

    2024-01-21 20:46:45
  • 访问javascript私有变量

    2010-01-19 13:56:00
  • Mysql中SQL语句不使用索引的情况

    2024-01-28 04:19:57
  • 利用标准库fractions模块让Python支持分数类型的方法详解

    2023-06-06 05:23:47
  • ASP的错误代码都有哪些?

    2009-10-28 18:15:00
  • 2008圣诞节网站特色Logo不完全点评

    2008-12-25 18:35:00
  • centos 6.9安装mysql的详细教程

    2024-01-13 06:51:15
  • 谈谈如何管理门户级网站的CSS/IMG/JS文件

    2009-09-03 11:48:00
  • python爬取豆瓣电影排行榜(requests)的示例代码

    2022-10-16 02:18:46
  • python 实现list或string按指定分段

    2023-10-30 02:04:20
  • asp之家 网络编程 m.aspxhome.com