前端JS文件中:
function laoding(){
var qqname = document.getElementById("qqname").value;
var qqnumber = document.getElementById("qqnumber").value;
$.ajax({
url: '/qq/home/', // **请求的URL链接 为空的话默认当前请求界面的URL**
type: "POST",
traditional: true, // 需要传递列表、字典时加上这句
data: { // JSON格式封装数据
'name': qqname,
},
success: function (data) {
console.log(data);
}, error: function (data) {
alert("请求错误!")
}
});
}
django 项目的urls.py
from django.contrib import admin
from django.urls import path,include
# from WeiXin.views import Wei_Xin
# from QQ.views import QQ_qq
urlpatterns = [
path('admin/', admin.site.urls),
# path('weixin/', Wei_Xin,name="Wei_Xin"),
# path('qq/home/', QQ_qq,name="QQ_qq"),
path('qq/', include("QQ.urls")), //这个是凡是qq开头的请求全部转到 QQ 这个app中的urls.py中,见下文 ,这里不转的话可直接写 : path('qq/home/', QQ_qq,name="QQ_qq"),
path('weixin/', include("WeiXin.urls")),
path('baidu/', include("BaiDu.urls")),
]
QQ app中的urls.py:
from django.urls import path,include
from QQ import views
urlpatterns = [
#path('weixin/', include()Wei_Xin,name="Wei_Xin"),
path('home/', views.QQ_qq,name="QQ_qq"),
]
QQ 的views.py 也就是数据处理模块如下:
from django.shortcuts import render
from django.http import HttpResponse
import json
# Create your views here.
def QQ_qq(request):
print(request)
string = '这里是QQ主界面'
name = request.POST.get("name") //接收 js文件传过来的 name参数
print(name)
return render(request,"home.html",{'string':string})