0

So I'm making a login system for my Django application. It works on my dev side absolutely fine, but in prod it doesn't work if I submit the correct username and password (It gives a 500 error). I'm not sure which of the two lines I've pointed out in views.py is the issue, but I figure it must be one of those two since if I do enter an incorrect user then I get redirected fine, even in prod.

The code on my dev and prod side are EXACTLY the same - the only difference is in the settings

   DEBUG = True
   ALLOWED_HOSTS = []

^ The above is my dev settings, but in prod DEBUG is False and ALLOWED_HOSTS has been filled out with the appropriate names.

In views.py:

def user_login(request):
if request.method == "POST":
    username = request.POST['username']
    password = request.POST['password']
    user = authenticate(username=username, password=password)
    if user is not None and user.is_active:
        login(request=request, user=user) <- This line
        return render(request, 'site/homepage.html') <- Or this line
    else:
        return render(request, 'site/login.html', {'incorrect': True})
else:
    return render(request, 'site/login.html', {})

In login.html:

{% block content %}
{% if user.is_authenticated %}
<div class="title">
    <h2>Error</h2>
</div>
<p>You have already logged in, {{ user.firstname }}. Please logout to login as another user!</p>
{% else %}
<div class="title">
    <h2>Login</h2>
</div>
{% if incorrect %}
<p>You have entered the wrong username and/or password.<br />
Please try again</p>
{% elif unauthorised %}
<p>You must login before trying to access that page!<br /></p>
{% endif %}
<form id="login_form" method="post" action="#">
{% csrf_token %}
<label>Username: <input type="text" name="username" value="" size="50" /></label>
<br />
<label>Password: <input type="password" name="password" value="" size="50" /></label>
<br />

<input type="submit" value="submit" />
</form>
{% endif %}
{% endblock %}
MGan
  • 49
  • 6
  • 2
    Which is the error message? – LostMyGlasses Feb 08 '16 at 10:46
  • 1
    What the webserver log say, e.g.: sudo tail /var/log/apache2/error.log – Aviah Laor Feb 08 '16 at 10:53
  • @AviahLaor [Mon Feb 08 11:21:54.582105 2016] [mpm_event:notice] [pid 1068:tid 3074058880] AH00491: caught SIGTERM, shutting down [Mon Feb 08 11:21:55.659160 2016] [mpm_event:notice] [pid 1439:tid 3074177664] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations [Mon Feb 08 11:21:55.659284 2016] [core:notice] [pid 1439:tid 3074177664] AH00094: Command line: '/usr/sbin/apache2' It's just me restarting the Apache server – MGan Feb 08 '16 at 11:28
  • Can you set DEBUG=True to capture the exception? – Aviah Laor Feb 08 '16 at 11:34
  • @AviahLaor If DEBUG=TRUE (Like it is in my dev code), there is no exception. The code works as intended and the redirect happens without a hitch. That's what is so weird about this error – MGan Feb 08 '16 at 11:36
  • try this logging middleware: import logging import sys from django.http impor Http404 class LogExceptions(object): def process_exception(self,request, exception): if isinstance(exception, Http404): return logging.critical(exception,exc_info=sys.exc_info()) return – Aviah Laor Feb 08 '16 at 11:41

2 Answers2

0

In the form, the action attribute doesn't have any value so you don't send anything to the view. It should be "."

<form id="login_form" method="post" action=".">

or

<form id="login_form" action="{% url 'login' %}" method="post">

if your login url name is login.

doru
  • 9,022
  • 2
  • 33
  • 43
0

Tested in my server this work, if not, tell us the new error.

view.py:

def login_user(request):
    if request.method == "POST":
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(request, user)
            return HttpResponseRedirect('/')
        else:
            return render(request, 'login/auth.html', {'incorrect': True})
    else:
        return render(request, 'login/auth.html', {})
Limbail
  • 1
  • 1
  • Nope, still getting a generic 500 error and nothing is coming up in any logs – MGan Feb 08 '16 at 12:09
  • Maybe something related to collectstatics or: http://stackoverflow.com/questions/15128135/setting-debug-false-causes-500-error And try to enable debug mode like ur dev system for show errors. – Limbail Feb 08 '16 at 12:33
  • @user2151557 Did you determine the issue on this 500 error. If so, what was it? I seem to be having a similar problem. – user3062149 Jun 29 '16 at 17:09