我正在开发一个Django应用程序,并通过自定义身份验证对用户进行身份验证。身份验证效果很好但是当我返回同一页面的渲染时,Django只是挂起。基本上我有一个POST请求URL auth digits
,如果成功,我将返回当前页面jubi
的呈现。我已经尝试过HttpResponseRedirect并且服务器调用了GET请求,但是没有重新加载页面(可能是因为它识别它已经在页面上了?)。
urls.py
urlpatterns = [
url(r'^index/$', views.index, name='index'),
url(r'^jubi/$', views.jubi, name='jubi'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
url(r'^auth_digits/$', views.auth_digits, name='auth_digits'),
]
views.py
def jubi(request):
return render(request, 'frontend/jubi.html',
{'user': request.user}, context_instance=RequestContext(request))
@csrf_exempt
def auth_digits(request):
apiUrl = request.POST.get('apiUrl', '')
credentials = request.POST.get('credentials', '')
dAuth = DigitsAuth()
user = dAuth.authenticate(apiUrl=apiUrl, credentials=credentials)
if user is not None:
user.backend = 'django.contrib.auth.backends.ModelBackend'
auth.login(request, user)
return render(request, 'frontend/jubi.html',
{'user': request.user}, context_instance=RequestContext(request))
else:
return HttpResponseRedirect('invalid')
jubi.html
{% if user.is_authenticated %}
<script>console.log("we are auth")</script>
{% else %}
<div class="load-login-form"></div>
{% endif %}