django云端留言板實例詳解
1.創(chuàng)建應(yīng)用
django-admin startproject cloudms cd cloudms python manage.py startapp msgapp
2.創(chuàng)建模板文件
在cloudms\msgapp\下創(chuàng)建templates文件夾,在templates文件夾下創(chuàng)建MsgSingleWeb.html(這里在pycharm中可以直接選擇new一個HTML file,會自動生成html,head,body等標簽)內(nèi)容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>云端留言板(1)首頁</title>
</head>
<body>
<h1>提交留言功能區(qū)</h1>
<form action="/msggate/" method="post">
{% csrf_token %}
發(fā)送方 <input type="text" name="userA" /><br>
接收方 <input type="text" name="userB" /><br>
消息文 <input type="text" name="msg" /><br>
<input type="submit" value="留言提交"/>
</form>
<h1>獲取留言功能區(qū)</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.設(shè)定url路由
本地路由。cloudms\msgapp\新建urls.py,內(nèi)容如下
from django.urls import path
from . import views
urlpatterns=[
path('',views.msgproc),
]
全局路由引入本地路由,cloudms\cloudms\urls.py內(nèi)容如下
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path("msggate/",include('msgapp.urls')),
path('admin/', admin.site.urls),
]
5.編寫views的交互函數(shù)
cloudms\msgapp\views.py內(nèi)容如下
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函數(shù)第三個參數(shù)是字典類型,表明向html頁面中特定變量賦值
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- django 自定義用戶user模型的三種方法
- 通過數(shù)據(jù)庫對Django進行刪除字段和刪除模型的操作
- Django中模型Model添加JSON類型字段的方法
- django模型層(model)進行建表、查詢與刪除的基礎(chǔ)教程
- 在Django的模型中添加自定義方法的示例
- 在Django的模型和公用函數(shù)中使用惰性翻譯對象
- Django 根據(jù)數(shù)據(jù)模型models創(chuàng)建數(shù)據(jù)表的實例
- linux環(huán)境下Django的安裝配置詳解
- django搭建項目配置環(huán)境和創(chuàng)建表過程詳解
- Django使用中間鍵實現(xiàn)csrf認證詳解
- django用戶登錄驗證的完整示例代碼
- Django框架自定義模型管理器與元選項用法分析
相關(guān)文章
解決Pycharm后臺indexing導(dǎo)致不能run的問題
今天小編就為大家分享一篇解決Pycharm后臺indexing導(dǎo)致不能run的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06
Python softmax實現(xiàn)及數(shù)值穩(wěn)定性詳解
這篇文章主要為大家介紹了Python softmax實現(xiàn)及數(shù)值穩(wěn)定性詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
python?爬取豆瓣電影短評并利用wordcloud生成詞云圖
這篇文章主要介紹了python?爬取豆瓣電影短評并利用wordcloud生成詞云圖,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-06-06
詳解基于python的全局與局部序列比對的實現(xiàn)(DNA)
這篇文章主要介紹了詳解基于python的全局與局部序列比對的實現(xiàn)(DNA).文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
python如何終止死循環(huán)和開啟死循環(huán)
這篇文章主要介紹了python如何終止死循環(huán)和開啟死循環(huán)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

