Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite works, but PostgreSQL migrated database causes ERROR - Django 3.0

Situation

  • I have built Django 3.0, PostgreSQL 11, macOS 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

  1. 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.
  2. 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 says Running 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
  3. 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.
  4. 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.
  1. I have also created an AWS RDS postgreSQL data base like the course leader, migrated and connected it with the server and in the settings butI still got the same error.
  2. I have looked in to user groups permissions as well and the source code SQLight database has no permissions given out in the admin tab same as my postgresql
  3. Created a brand new Django 3 project, brand new Virtual Environment 0 I have only copied everything in as text from the previous project, migrated everything fromt eh previouse database, I get the same error
like image 525
sogu Avatar asked Dec 14 '22 09:12

sogu


1 Answers

Not sure I can help, but I have a Mac, use Django and had some problems with PostgreSQL, so I'll share my ideas.

First, it seems to me that the problem is indeed the connection with the database, not the code. I looked up your error in the Django code, it says:

    def _get_response(self, request):
        """
        Resolve and call the view, then apply view, exception, and
        template_response middleware. This method is everything that happens
        inside the request/response middleware.
        """
        ...

        # Complain if the view returned None (a common error).
        if response is None:
            ...

            raise ValueError(
                "The view %s.%s didn't return an HttpResponse object. It "
                "returned None instead." % (callback.__module__, view_name)
            )

Although I'm certainly not an expert on middleware, apparently Django is not receiving data from your database.

I once had problems with different versions of PostgreSQL on my Mac. You can check in /Library/PostgreSQL, I have two directories (11 and 12), so I have to be careful which one I use for my projects. In PgAdmin you can see both versions. If you have old versions or want to reinstall PostgreSQL (which is pretty drastic but would give you a clean sheet), there is a good manual here: https://medium.com/@zoefhall/effectively-uninstall-and-reinstall-psql-with-homebrew-on-osx-fabbc45c5d9d. You can check the version that your Django is using by typing in your Shell:

python3 manage.py shell -c "from django.db import connection; print(connection.cursor().connection.server_version)"

For me the result is 110005, which translates into 11.0.5.

I also had problems with psycopg2 several times. Installation of it is necessary when you set up your virtual environment with Django, so if you managed that, then probably that went ok. The problem I had was that it couldn't find the config file, and the easiest solution for me was to install the binary version psycopg2-binary. But if you want a specific version of PostgreSQL on your Mac, then you need to put the pg_config file in your PATH, see https://www.psycopg.org/docs/install.html.

That's what I have learned, I hope it helps you a little bit.

like image 189
Paul Rene Avatar answered Apr 28 '23 02:04

Paul Rene