我遇到的问题是,执行Ajax从html页面获取后,以下视图函数指令没有执行。
这是model.py
from django.db import models
from django.utils import timezone
class user(models.Model):
FB_name=models.CharField(max_length=20)
FB_link=models.URLField()
FB_usrname=models.CharField(max_length=50)
FB_usrID=models.IntegerField()
FB_email=models.CharField(max_length=100)
reg_date = models.DateTimeField(default=timezone.now)
def __unicode__(self):
return "{0} {1} {2} {3} {4}{5}".format(self,self.FB_name, self.FB_link, self.FB_usrname,self.FB_usrID,self.FB_email)
Views.py
from django.shortcuts import render
from django.core.context_processors import csrf
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import View
from django.utils import timezone
from django.http import HttpResponse
from .models import user
class login_call(View):
def get(self, request):
return render(request, 'login/login.html', {})
class user_details(View):
def get(self, request):
if request.GET :
usrID = request.GET['FB_usrID']
me=user.objects.filter(FB_usrID=usrID).values()
if not me:
hello=user.objects.create(FB_name=request.GET['FB_name'],
FB_link=request.GET['FB_link'],
FB_usrname=request.GET['FB_usrname'],
FB_usrID=request.GET['FB_usrID'],
FB_email=request.GET['FB_email'],
reg_date=timezone.now())
hello.save()
response = 0
else:
response = 1
return render(request,'homepage/hom.html')
return HttpResponse('response', content_type="application/json")
class dashboard(View):
def get(self, request):
return render(request,'homepage/hom.html')
urls.py
from django.conf.urls import include, url
from django.contrib import admin
from event.views import user_details
from event.views import login_call
from event.views import dashboard
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^user_details/$',user_details.as_view()),
url(r'',login_call.as_view()),
url(r'^home$',dashboard.as_view()),
]
最后是GET jquery函数: -
$.ajax({
url:'/user_details/',
type: 'GET',
cache:'false',
data: {
FB_usrID:'554113199893',
FB_name:'Tharun',
FB_link:'tharun@someshit.com',
FB_usrname:'tharun cow',
FB_email:'tharun@gmail.com',
},
success:function(response){
if(response == 0)
{
document.getElementById("message").innerHTML += "<br>Sent";
}
else
{
document.getElementById("message").innerHTML += "<br>Fail";
}
},
complete:function(){},
error:function (xhr, textStatus, thrownError){}
});
点击该功能后,数据将使用ajax-get传递给服务器 服务器响应: -
"GET /user_details/?FB_usrID=554113199893&FB_name=Tharun&F
B_link=tharun%40someshit.com&FB_usrname=tharun+lawrence&
FB_email=tharun%40gmail.com HTTP/1.1" 200 11476
这个ajax-get的目的是通过jquery fb api将Facebook用户详细信息发布到django以存储在db中并对主页进行身份验证。
答案 0 :(得分:3)
您的user_details(View).get()
方法以return
语句开头。显然,这种方法的执行将停在那里 - 这是return
的含义。
另外(完全不相关但......):GET请求绝不能修改服务器的状态。这是POST / PUT / PATCH和DELETE的用途。