Situation
- I have built Django 3.0 project with a couple of applications.
- I have created the accounts app based on following course and it's github
- Than I have created an application fro authentication
acc
- All this has been done in an SQLite database
- Previously I have tried out a PostgreSQL database for the early application that was working fine
- but now when I switch of in the settings.py file the SQLite to PostgreSQL I get an error i I try to log in
- If I switch back the settings.py to SQLite everything works perfectly (ex.: authentication, logging in with user, user doing things on the website with it's own settings)
- I use decorators.py to keep logged in users visiting the login and signup pages and that gives error when I switch to postgresql. I only use here
HttpResponse
that the error message contains
decorators.py
from django.http import HttpResponse
from django.shortcuts import redirect
def unauthenticated_user(view_func):
def wrapper_func(request, *args, **kwargs):
if request.user.is_authenticated:
return redirect('home')
else:
return view_func(request, *args, **kwargs)
return wrapper_func
def allowed_users(allowed_roles=[]):
def decorator(view_func):
def wrapper_func(request, *args, **kwargs):
group = None
if request.user.groups.exists():
group = request.user.groups.all()[0].name
if group in allowed_roles:
return view_func(request, *args, **kwargs)
else:
return HttpResponse('Authorized')
return wrapper_func
return decorator
ERROR
If I log in while settings.py uses PostgreSQL. If I log out everything works out fine again. If I use SQL lite I can log in and everything works perfectly
ValueError at /
The view accounts.decorators.wrapper_function didn't return an HttpResponse object. It returned None instead.
Request Method: GET
Request URL: http://localhost...
Django Version: 3.0
Exception Type: ValueError
Exception Value: The view accounts.decorators.wrapper_function didn't return an HttpResponse object. It returned None instead.
Exception Location: /Users/.../python3.7/site-packages/django/core/handlers/base.py in _get_response, line 126
Python Executable: /Users/.../bin/python3
Python Version: 3.7.3
.....
Request information
USER MYUSERNAME
GET No GET data
POST No POST data
FILES No FILES data
COOKIES ...
...
Tried to Solve
- The guide that I follow created user groups that I have done as well in my migrated postgreSQL database, but I have still received the same error as USER1 in the comment section.
- This was the recommendation in the bottom section of the video
- "USER1 i find it, i forgot to change the user's group!
- --> USER2 go to admin panel and in you user section add customer in the chosen group section".
- I have done exactly that and it did not worked the only difference is that I have used a migrated postgresql and they used the original SQLight that if I use than the whole thing works for me as well, but I want to make it work with PostgreSQL.
- I have data, tables in both database but PostgreSQL for some old staff and SQLite for everything.
- I have tried to migrate the SQLite to PostgreSQL with this guide.
- I have successfully created a copy of the SQLite database
- but when I changed the settings to postgres and I try to
python manage.py migrate
it saysRunning migrations: No migrations to apply.
python manage.py loaddata db.json
- The users are migrated (I can log in with them and get error just like with the only SQlite users, if I mistype the user or the password it does not lets me in) from SQLite but I don't see any of the data tables in Postgresql if I look it up with an IDE
- I have talked to other people on forums abut this many said that it is the decorator file that is problematic but It exactly occurs only at data base switching.
- I have created a new postgresql database and I have tried to migrate everything (the migration did not migrate everything already). Than I have tried to sign up with a new account an it gave me the following error message after filling out the form an pressing submit
DoesNotExist at /register/
Group matching query does not exist.
So here is how I have solved it.
Home › Authentication and Authorization › Groups › customer