Superuser Login in Admin Site

Hello, I am currently trying to create a web app where thir will be users that can login. for that, I tried creating a super user to begin. The creation process worked fine but I wasn’t able to login at the admin webpage when running the app itself with Docker. So I tried using py manage.py runserver and tried connecting to the admin page through that. That worked.

I need to create users though and that means I can’t really do it from the development server. (Since I’m new to this I tried and the wabapp login page said the credentials were invalid). So I d’ont really know what could possibly have gone wrong with authentication in the app.

I’ve read somethings that say this could be caused by wsgi but I couldn’t find a way to solve it yet.

For info, I created the superuser using py manage.py createsuperuser and not using my own model. I’m willing to share some code with you, but since I don’t know which parts are relevant, I’d rather not flood the page with useless code.

Ok, lets try to break this down a little:

Please provide more details about what you mean here - this includes being more precise about “the creation process” and “running the app itself with Docker”. (What commands you used, what your dockerfile contains, how you’re running the container, and any other relevant information about this.)

As opposed to what server? How many servers are involved here, and what are their roles?
How many databases are involved? Are they different as well?

How are you trying to create users in your “non-development” server?

I don’t think we need to see any of your actual code yet - unless there’s some involved in how you’re trying to create users. But be more specific about what commands you’re running, whether or not you’re receiving any errors, what systems, servers, and databases are involved, and what general code you’ve written as opposed to using code and scripts supplied with Django.

So:

Please provide more details about what you mean here - this includes being more precise about “the creation process” and “running the app itself with Docker”.

For the creation process, I mean I used python manage.py createsuperuser to create the super user and then entered the necessary information and got a success message. Once that was done, I used python manage.py runserver and connected to http://127.0.0.1:8000/admin/to get the admin page. Login worked great there.
About what I mean with running with docker, when I compose up the docker-compose file, and go my webpage/admin/ and try to login, it doesn’t work.

FROM python:3.6


COPY manage.py gunicorn-cfg.py requirements.txt .env ./


RUN pip install -r requirements.txt

COPY app app
COPY authentication authentication
COPY core core

RUN python manage.py makemigrations 
RUN python manage.py migrate

EXPOSE 5005
CMD ["gunicorn", "--config", "gunicorn-cfg.py", "core.wsgi"]

Then,

As opposed to what server? How many servers are involved here, and what are their roles?
How many databases are involved? Are they different as well?
How are you trying to create users in your “non-development” server?

What I mean is that I tried creating a user using http://127.0.0.1:8000/admin/ and trying to use it at mywebpage (I have a user login page to access the webpage) but it didn’t work.

And for the final things,

But be more specific about what commands you’re running, whether or not you’re receiving any errors, what systems, servers, and databases are involved, and what general code you’ve written as opposed to using code and scripts supplied with Django.

When I try to login to mywebpage/admin/ I get the message thet says I have to use valid username and password.
I’m currently on Windows 10 and using Visual Studio code. I have a database with completetly unrelated data that I use for my webpage .

And on extra, since I’m using a django&docker template, a lot of the stuff was already written down. But there seems to be an ‘authentication’ app and whenever I try to run migrations on it, I get the following error:

#11 0.526 Traceback (most recent call last):
#11 0.526   File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
#11 0.526     return self.cursor.execute(sql, params)
#11 0.526   File "/usr/local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
#11 0.526     return Database.Cursor.execute(self, query, params)
#11 0.526 sqlite3.OperationalError: no such table: auth_user

I hope that makes some things more clear?

Not really - there are a lot of questions above that you either skipped completely or just glossed over.
General descriptions are not helpful at this point. A listing of specific commands used, scripts being run, etc, are.

As just the first example, I asked for specifics in terms of “running the app itself with Docker” - what is the full docker command you used to do this? Other examples include my request for specifics regarding the database(s) you are using. You also again state:

Saying “it didn’t work” doesn’t provide any useful information - saying

does - but it’s not clear by how you’ve worded this whether this applies to your previous statement about it not working, or whether this was a different attempt.

Ok I’ll try again.

I’m using Visual Studio Code with a Dockerfile and docker-compose file. To compose the docker, I have a simple right-click on the docker-compose.yml file which opens up a menu with “Compose Up” and that’s how I launch the Docker. There is also “Compose Down” and “Compose Restart”.

I have one database that I fill up with API requests with a button click as well as the default database.

DATABASES = {
    'data': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '../data.sqlite3',
    },
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '../auth.sqlite3',
    }
}

I can show you the way I fill it up but it takes about 400 lines of code so I’ll abstain for now. And I had to create a volume for data.sqlite3 since I need data to persist.
When I run the Docker Compose Up, I access the website and do everything I want it to do. No errors. Now, I would like to add users and login to restrict access to some data of the data.sqlite3 database.

Since the template as started with used had an authentication app with login and register pages, as well as working code to authenticate, I want to use those. I am able to create a user from these views and login using the user created like so (until I restart the Docker). However, I need to create users and permissions. I tried hardcoding a user into the authentication/models.py file:

user = User.objects.create(username='test', email='test@test.com', password='test')
user.is_superuser = True
user.is_active = True
user.is_staff = True
user.save()

However, it I can’t actually login to my site with this user. To try and correct this, I decided to use

python manage.py createsuperuser

and created a superuser successfully. Then I used “Docker Compose Up” (through the right click menu) and went to the “localhost:5005/admin/” page to try and login using that super user I had just created. It did not work. I get the following screen:
admin
Following this, and with some research on Internet, I tried using

python manage.py runserver

and went to " http://127.0.0.1:8000/admin/". There I tried to login using the super user created earlier. This worked and I was able to see Users and Groups. I could also see the user “test” I had hardcoded previously.

I then created a second test user “test2” within the admin page.
I then used Docker Compose Up again and tried to login (to my website) with “test2” but to no avail.

Since the app “authentication” wasn’t in the list of INSTALLED_APPS, I tried adding it to there, maybe the problem had something to do with migrations. However, when I used Docker Compose Up, I got an error during the migrations for the app “authentication”.

#11 0.526 Traceback (most recent call last):
#11 0.526   File "/usr/local/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
#11 0.526     return self.cursor.execute(sql, params)
#11 0.526   File "/usr/local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
#11 0.526     return Database.Cursor.execute(self, query, params)
#11 0.526 sqlite3.OperationalError: no such table: auth_user

I also have another app “app” with has everything related to my website, other than authentication (and contains the data.sqlite3 database). To be able to use the manage.py createsuperusercommand I had to create a virtual environment so I have a folder with that, on top of the .env file that came with the template.

I have now managed to change the error (during makemigrations for ‘authentication’ app) since it seemed that my path was a little wrong. I changed the path for the ‘default’ database to it’s absolute path. I know get the following error:

 > [7/8] RUN python manage.py makemigrations:
#11 0.519 Traceback (most recent call last):
#11 0.519   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 217, in ensure_connection
#11 0.519     self.connect()
#11 0.519   File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 195, in connect
#11 0.519     self.connection = self.get_new_connection(conn_params)
#11 0.519   File "/usr/local/lib/python3.6/site-packages/django/db/backends/sqlite3/base.py", line 194, in get_new_connection
#11 0.519     conn = Database.connect(**conn_params)
#11 0.519 sqlite3.OperationalError: unable to open database file

I am able to open the databse (via DB Browser for SQLite) and can see the users I had created before.

I managed to make the login work on my website. It seemed the problem cam from the ‘default’ database which stored the users/groups/permissions. By moving the database and changing the path, I was able to fix this problem:
I moved the auth.sqlite3 file into the authentication app and changed the path to “/authenticate/auth.sqlite3”

I also had to create a volume for the auth.sqlite3 database to make sure that users created were persistant when the Docker closed.