JQUERY AJAX例子(数据,文件上传)

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r'^test/$', views.test, name='test'),
    url(r'^ajax_jquery_test/', views.ajax_jquery_test, name='ajax_jquery_test'),
]

template-ajax_jquery_test.html




    
    









views.py

def test(request):
    return render(request, 'ajax_jquery_test.html')


def ajax_jquery_test(request):
    res = {'status': True, 'data': 'ok'}
    user = request.POST.get('user')
    obj = request.FILES.get('obj')

    import os
    file_path = os.path.join('static', obj.name)
    with open(file_path, 'wb') as f:
        for i in obj.chunks():
            f.write(i)

    import json
    return HttpResponse(json.dumps(res))

你可能感兴趣的:(JQUERY AJAX例子(数据,文件上传))