jquery - Django: accept AJAX call and render response? -
here's javascript code:
function getcookie(name) { var cookievalue = null; if (document.cookie && document.cookie != '') { var cookies = document.cookie.split(';'); (var = 0; < cookies.length; i++) { var cookie = jquery.trim(cookies[i]); // cookie string begin name want? if (cookie.substring(0, name.length + 1) == (name + '=')) { cookievalue = decodeuricomponent(cookie.substring(name.length + 1)); break; } } } return cookievalue; }; var csrftoken = getcookie('csrftoken'); //ajax call function csrfsafemethod(method) { // these http methods not require csrf protection return (/^(get|head|options|trace)$/.test(method)); } $.ajaxsetup({ crossdomain: false, // obviates need sameorigin test beforesend: function(xhr, settings) { if (!csrfsafemethod(settings.type)) { xhr.setrequestheader("x-csrftoken", csrftoken); } } }); $(".color_wheel").click(function() { $.ajax({ url:'/', type: "post", data: {num : level}, });});
and here's views file:
from django.shortcuts import render .models import joke # create views here. def home_page(request): return render(request, 'home.html') def joke_page(request): if request.post: num = request.post['num'] return render(request, 'joke.html', {'joke': joke.find_joke(num)})
as can see, want django accept ajax call, take given number(num) , use display new page. thing is, doesn't work... maybe i'm doing wrong, maybe whole thing wrong... please if know how. thank you!
edit:
urls.py
from django.conf.urls import url jokes import views urlpatterns = [ url(r'^$', views.home_page, name='home_page'), url(r'^(?p<joke_id>[0-9]+)/$', views.joke_page, name='joke_page'), ]
i think can done using get
request
$(".color_wheel").click(function() { window.location.href = "/"+devel, })
in view
def joke_page(request, joke_id): return render(request, 'joke.html', {'joke': joke.find_joke(joke_id)})
Comments
Post a Comment