4

Got a problem with Basic authentication in Django REST framework when debug mode is turned off. I am using Django 1.8.4 and Django REST Framework 3.2.2.

Looks like it saves credentials for all computers with the same IP address when the first is logged in. But after some time it prompts for the username and password again.

However, this problem does not occur when the debug mode in Django REST framework settings is set to True. I would like to have the same behaviour when debug is turned off. What is causing the problem?

user3618276
  • 91
  • 2
  • 10

1 Answers1

3

In settings.py file add the host/domain-name of the system to which django is allowed to serve.

Use:

ALLOWED_HOSTS = ['127.0.0.1'] or ALLOWED_HOSTS = ['localhost'] so that django can serve the localhost.

You may also add other IP address as you wish.

Example:

ALLOWED_HOSTS = ['127.0.0.1', '192.1.12.23']

Domain names can also be accepted:

ALLOWED_HOSTS = ['www.example.com']

If you wish to serve many hosts, you may simply use *, as shown:

ALLOWED_HOSTS = ['*']

This will serve Django to any host in the world.

Ihor Pomaranskyy
  • 5,437
  • 34
  • 37
troy_achilies
  • 592
  • 1
  • 7
  • 15
  • 1
    I have my domain-name of the system in ALLOWED_HOST setting. And it does not work. Even when changed to ALLOWED_HOSTS = ['*'] it does not work either. – user3618276 Jan 18 '17 at 12:37
  • are you talking about simple login by username and password, when you say problem on basic authentication? – troy_achilies Jan 18 '17 at 12:40
  • Can you provide the Source Code (with requirements.txt) ? –  Jan 18 '17 at 13:07
  • Here is the important code (since I can't provide the full): http://pastebin.com/UfSgx31M – user3618276 Jan 18 '17 at 13:34
  • are you using django sessions for login? – troy_achilies Jan 18 '17 at 17:30
  • Yes, `django.contrib.sessions.middleware.SessionMiddleware`. Deleting the session entries from the django_session table doesn't help. Session timetout to zero neither. Probably it is saving sessions somewhere else and using that one. – user3618276 Jan 19 '17 at 08:53
  • will you please check where is it saving session and let me know, also let me know if you need help finding where the session is stored – troy_achilies Jan 19 '17 at 11:20
  • Can't find any information where exactly it is saving, because it looks like Django does it automatically. No change in configuration helped it. In case the debug mode is true, it somehow does not save the session, because it works – user3618276 Jan 19 '17 at 12:22
  • Do you have a Apache /nginx server running in front of django? – Karan Kumar Jan 19 '17 at 17:16
  • Yes, but it also does not work on a Django development server on localhost, so I do not think that is the problem – user3618276 Jan 24 '17 at 08:11