python - 如何通过 Ajax jQuery 将简单数据发送到 Django 中的views.py?
问题描述
我想使用 Ajax jQuery 将 home.html 中的简单数据(数字)发送到 views.py 中的函数。但似乎该函数没有被调用。
home.html: 我正确地得到了成功响应。我在成功通知上看到了 tagID。我想在我的views.py中看到它
...
function GetInfo(e){
document.getElementById("test3").innerHTML = e.target.myCustomID;
var tagID = e.target.myCustomID;
$.ajax({
url: 'Ajax1',
type: "POST",
data: {
'tagID': tagID,
'csrfmiddlewaretoken': '{{ csrf_token }}',
},
success: function (data) {
alert ("Congrats! You sent some data: " + tagID);}
,
error: function() {
alert ("Something went wrong");
}
})};
views.py: 我想看看这个函数是由ajax调用的。因此,如果命令 print ("AAAAAAAAAAAA") 有效,我很高兴!
...
@csrf_exempt
def Ajax1(request):
print ("AAAAAAAAAAAAA")
if request.is_ajax() and request.POST():
print("BBBBBBBBBBBBB")
TagID = request.POST.get('tagID', None)
else:
raise Http404
我的应用程序/urls.py
urlpatterns = [
url(r'', views.home_map, name="home"),
url(r'^ajax/$', views.Ajax1, name="Ajax")
]
网址.py
urlpatterns = [
path(r'', include('my_app.urls')),
path('admin/', admin.site.urls),
path(r'^ajax/$', Ajax1, name='Ajax')
]
你能告诉我我错过了什么吗?谢谢
更新
根据善意的建议,我做了一些改变。这仍然行不通。
**views.py:**
def ajax1(request):
TagIDD = request.POST['object_type']
print("AAAAAAAAAAAAAAAAA", TagIDD)
if request.is_ajax() and request.method == "POST":
print("BBBBBBBBBBBBB")
TagID = request.POST.get('tagID')
else:
raise Http404
# TagID = request.POST('tagID')
print ("Hello")
print(TagID)
response = {
'TagID': TagID
}
return HttpResponse(response)
主页.html
function GetInfo(e){
document.getElementById("test3").innerHTML =
e.target.myCustomID;
const tagID = e.target.myCustomID;
$.ajax({
type: "POST",
url: "{{ 'ajax-view/' }}",
data: {
'csrfmiddlewaretoken': '{{ csrf_token }}',
'tagID': tagID
},
success: function (data) {
alert ("Congrats! You sent some data: " +
tagID);}
,
error: function() {
alert ("Something went wrong");}
})
};
</script>
my_app\urls.py:
urlpatterns = [
url(r'', views.home_map, name="home"),
url('ajax-view/', views.ajax1, name='ajax-test-view')
]
网址.py:
urlpatterns = [
path(r'', include('wrms01_map.urls')),
path('admin/', admin.site.urls),
]
尽管如此,ajax 似乎没有在views.py 中调用函数“ajax1”。但我可以看到成功通知。提前致谢。
解决方案
你做错了ajax调用。您必须在 url 中传递端点的EXACT URL:''
您所做的是传递一个字符串“Ajax1”,在您的 urls.py 中找不到该字符串。在您的 urls.py 中,您有 'ajax' 作为 url 模式,而 Ajax1 是您的视图函数名称。
另外,我在您的 urls.py 中观察到,对于 2 种不同的视图方法,您具有相同的模式(这是一种不好的做法)。
现在在你的ajax里面做这个
$.ajax({
url: "{%url 'Ajax'%}", //This is the jinja template tag that will automatically fill in the url pattern from your urls.py corresponding to the name of that url pattern
type: "POST",
data: {
'tagID': tagID,
'csrfmiddlewaretoken': '{{ csrf_token }}',
},
success: function (data) {
alert ("Congrats! You sent some data: " + tagID);}
,
error: function() {
alert ("Something went wrong");
}
})
推荐阅读
- android - Android COIL 加载夜间占位符可在灯光模式下瞬间绘制
- azure-data-factory - Azure 数据工厂触发器:管道必须在每年 12 月的第二个星期五从月底触发
- json - JAVA Spring boot如何在Postman中发布arraylist
- c - 创建新的循环链接列表时出现分段错误
- arrays - props 中的过滤器数组
- python - 如何将更改名称的图片保存到colab中的谷歌驱动器?
- android - 导航图范围内的 ViewModel 未在菜单导航(抽屉)事件中得到清理
- java - 使用 JPARepository 和 UserService 更新用户详细信息时遇到问题
- java - 使用 Swing UI 设计器在 JTabbedPane 中创建 JTable - Intellij
- python - Pygame Zero:让玩家“Gameover”后如何向 Pygame Zero 添加 Surface