Can anyone help? I'm following a tutorial to make a basic blog site using Django and PostgreSQL and I can't add a database

49 Views Asked by At

I've added a 'templates' folder into the blog directory but I can't get the Django database urls to recognise it. I think I need to add my url into the settings.py file but I have no idea how I'm supposed to do that.

When I try to run the server I get this error:

Traceback (most recent call last):
  File "/workspace/django-blog/manage.py", line 22, in <module>
    main()
  File "/workspace/django-blog/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/django/core/management/__init__.py", line 363, in execute
    settings.INSTALLED_APPS
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/django/conf/__init__.py", line 82, in __getattr__
    self._setup(name)
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/django/conf/__init__.py", line 69, in _setup
    self._wrapped = Settings(settings_module)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/django/conf/__init__.py", line 170, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/gitpod/.pyenv/versions/3.12.1/lib/python3.12/importlib/__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "/workspace/django-blog/codestar/settings.py", line 91, in <module>
    'default': dj_database_url.parse(os.environ.get("DATABASE_URL"))
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspace/.pyenv_mirror/user/current/lib/python3.12/site-packages/dj_database_url/__init__.py", line 126, in parse
    raise ValueError(
ValueError: No support for 'b'''. We support: cockroach, mssql, mssqlms, mysql, mysql-connector, mysql2, mysqlgis, oracle, oraclegis, pgsql, postgis, postgres, postgresql, redshift, spatialite, sqlite, timescale, timescalegis

I can't understand the problem.

The server was running completely fine until I added the templates folder. The folder contains an html file with a template for the blog posts which was provided by the tutorial, which also suggests that when I add:

from django.shortcuts import render
from django.views import generic
from .models import Post

# Create your views here.
class PostList(generic.ListView):
    queryset = Post.objects.all()
    template_name = "post_list.html"

to the views.py file the server should display the post but that's when I get the error in the terminal.

I've tried to add the templates/ directory into the databases in settings but I don't know the correct format for it. I've even asked ChatGPT but even that seems to be stumped, probably because I've no idea what I'm meant to be asking.

1

There are 1 best solutions below

1
komal sai On

From the error statement provided by you, problem is with DATABASE_URL in .env file . To make it work ,

  1. check postgress up and by running sudo systemctl status postgresql (linux).make sure to create a database.
psql
CREATE DATABASE yourdatabase;
CREATE USER youruser WITH PASSWORD 'yourpassword';
ALTER DATABASE yourdatabase OWNER TO youruser;

  1. make sure to install dependencies for postgress psycopg2==2.9.9 , psycopg2-binary==2.9.9 cross check postgress configuration with settings.py file
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': DATABASE_NAME,
        'USER': DATABASE_USER, 
        'PASSWORD': DATABASE_PASS,
        'HOST': DATABASE_URL,  //'localhost'
        'PORT': DATABASE_PORT,  // default port '5432' 
    } 
}

.env file

DATABASE_PORT= '5432'
DATABASE_URL= 'localhost'
DATABASE_NAME = 'yourdatabase'
DATABASE_USER = 'youruser'
DATABASE_PASS = 'yourpassword' ```

It should make connection with database .