1

I'm working on a Django app and am authenticating users via a custom authentication. Authentication works great but when I return the rendering of the same page, Django just hangs. Essentially I have a POST request url auth digits and if success I return the rendering of the current page jubi. I've tried a HttpResponseRedirect and the server calls the GET request, but doesn't reload the page(maybe because it recognizes it is already on the page?).

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 %} 
Ulises Giacoman
  • 522
  • 4
  • 21
  • What do you mean when you say Django hangs? – Keenan Lawrence Jul 25 '16 at 15:42
  • @KeenanLawrence In the console, instead of seeing a 301 redirect or a 200 GET request, no new info is displayed as if the render request was just ignored. Does that make sense? – Ulises Giacoman Jul 25 '16 at 15:45
  • I see. That is strange... Debug doesn't give any info as well? – Keenan Lawrence Jul 25 '16 at 15:52
  • No info, in my custom authentication I make a get request and I thought it may be a concurrency issue but not it's going in order. The really weird part is when I use the HttpRedirectResponse the console will log the GET request to `jubi`, but the page will not load, as if it is simply downloading the page and saving it for later. – Ulises Giacoman Jul 25 '16 at 15:55
  • What's the status code of the GET? 200? Also, is this on successful authentication? What happens when the authentication fails? – Keenan Lawrence Jul 25 '16 at 16:04
  • Yes, on successful authentication we call the GET and it returns 200. If authentication fails we redirect an invalid page. Unfortunately, neither redirects actually load the page in the browser, they both appear as 200s in the console, I can see the requests being made in Chrome's dev tools, but there is no visual change on the page (no refresh). – Ulises Giacoman Jul 25 '16 at 16:09
  • may be you should try this approach? http://stackoverflow.com/questions/2095520/fighting-client-side-caching-in-django – Compadre Jul 25 '16 at 17:06
  • I have this exact issue when using Dropzone.js. The `return render(...)` statement is executed, but the page is not re-rendered. When using a simple HTML5 file input, it works perfectly. – lostsoul29 Oct 06 '16 at 23:30

0 Answers0