我正在尝试使用ajax构建一个接受输入的web应用程序。输入被发送到Django服务器端,我很难获得正确重定向的url。我知道目前的问题与django翻译应用程序有关。在
以下是相关章节。在
Ajax$(document).ready(function(){
$('#applicant_form').on('submit', function(event){
event.preventDefault() //this will stop the file from submitting the form manually.
$.ajax({
url :'/save_applicant/', // the endpoint
type : "POST", // http method
data : { the_post : "Yay" }, // data sent with the post request
dataType: "json",
// handle a successful response
success : function(json) {
console.log("success"); // another sanity check
},
// handle a non-successful response
error : function(xhr,errmsg,err) {
$('#results').html("
" ×
console.log(xhr.status + ": " + xhr.responseText); // provide a bit more info about the error to the console
}
});
});
});
HTML/DJANGO模板
^{pr2}$
视图.pydef save_applicant(request):
if request.method =='POST':
id = None
if 'applicant' in request.session:
id = request.session['applicant']
applicant_form = ApplicantForm(request.POST)
if applicant_form.is_valid():
session_applicant = applicant_form.save(commit=False)
if id:
session_applicant.id = id
else:
session_applicant.save()
request.session['applicant'] = session_applicant.id
return HttpResponse(json.dumps({'status':'applicant_saved'}), content_type='application/json')
else:
return HttpResponse(applicant_form.errors.as_json(),content_type='application/json')
else:
return HttpResponse(json.dumps({'status':'not_post'}),content_type='application/json')
网址.pyfrom django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls.i18n import i18n_patterns
urlpatterns = [
]
urlpatterns += i18n_patterns(
url(r'^$', 'plan_b_profile.views.home', name='home'),
url(r'^save_applicant/','plan_b_profile.views.save_applicant',name='save_applicant'),
url(r'^admin/', include(admin.site.urls)),
)
命令行[21/Aug/2015 20:53:43]"POST /save_applicant HTTP/1.1" 302 0
[21/Aug/2015 20:53:43]"GET /en/save_applicant/ HTTP/1.1" 200 22
再次感谢!在