Fork me on GitHub

python功能

1.前端使用ajax

项目名字/views.py

from django.shortcuts import render
from django.http import JsonResponse
from pythonweb.models import Mobile

# Create your views here.
def index(request):
    print(1)
    str = Mobile.objects.all()
    return render(request, 'index.html', {'str': str})

def ajax_list(request):
    print(2)
    a={'key':'123'}
    return JsonResponse(a)

app/urls.py

from django.contrib import admin
# from django.urls import urlpatterns
from pythonweb import views
from django.conf.urls import url
# from pythonweb import views

urlpatterns = [
    url('admin/', admin.site.urls),
    url('views/', views.index),
    url(r'^ajax_list/$', views.ajax_list),
]

html

    $(function(){
        $.ajax({
            url:'/ajax_list/',
            dataType:'json',
            success:function(data){
                console.log(data)
            }
        })
    })

  

 

 

posted @ 2018-09-03 09:56  zhang_yx  阅读(276)  评论(0编辑  收藏  举报