jQuery与django传参

Posted JohnRey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jQuery与django传参相关的知识,希望对你有一定的参考价值。

Get方式传参

Django中的代码如下:

  • urls.py代码:
from django.conf.urls import url
from django.contrib import admin
import AjaxTest.views

urlpatterns = [
    url(r‘^admin/‘, admin.site.urls),
    url(r"^index/$",AjaxTest.views.index),
]
  • views.py代码:

 

from django.http import HttpResponse

def index(req):
    print req.GET.get(‘url‘)
    if req.GET.get(‘url‘)==‘test‘:
        return HttpResponse("hello,this is a test")
    else:
        return HttpResponse("hahahaha")

jQuery中的代码如下:

  • 方式1:
$("input").click(function() {
	$.get("/index/?url=test", function (response, status, xhr) {
		$(".box").html(response);
	});
});
  • 方式2:
$("input").click(function() {
    $.get("/index/", "url=test", function (response, status, xhr) {
        $(".box").html(response);
    });
});
  • 方式3:

 

$("input").click(function() {
	$.get("/index/",{
		url:"test"
	},function(response,status,xhr){
		$(".box").html(response);
	});
});

 

  

 

以上是关于jQuery与django传参的主要内容,如果未能解决你的问题,请参考以下文章

如何在Django中的jQuery发布后呈现上一页

Django + Axios & Ajax post和get 传参

django跳转页面传参

Django中基于函数的视图和基于类的视图以及路由传参

Django:管理员中的 AJAX/jQuery

管理中的 Django/jQuery 级联选择框