2

I am attempting to use Django + Heroku + all necessary dependencies to create my app.

After following these steps :

migrating an existing django project

However I keep getting this error when I ran python3 manage.py runserver:

import dj_database_url ImportError: No module named 'dj_database_url'

I have tried to fix it with these instructions and this

THIS is my code :

I imported the dj-database-url

import dj_database_url

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

I added the follow STATIC assets necessitities

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

THIS is in my requirements.txt file

dj-database-url==0.4.2

gunicorn==19.7.1

whitenoise==3.3.0

I am still getting the ImportError. How do I fix this?

Waltham WECAN
  • 481
  • 4
  • 11
  • 26
  • is this error on the local server or just on Heroku ? – Sachin Aug 30 '17 at 16:49
  • @SachinKukreja I am not sure. The error path is this : `/Users/iivri.andre/virtualenvironment/new_app/Tut/Tut/settings.py, line 14,` I think it is local – Waltham WECAN Aug 30 '17 at 18:59
  • output of `pip freeze` contains `dj-database-url`? installed it in virtual environment? restarted gunicorn? tried with django development server? – Sachin Aug 30 '17 at 19:22
  • @SachinKukreja After I followed these steps : [migrate existing django project](https://devcenter.heroku.com/articles/django-app-configuration#migrating-an-existing-django-project) I ran `python3 manage.py runserver` so I am not sure I did anything to `gunicorn` – Waltham WECAN Aug 30 '17 at 19:52
  • Don't forget to commit changes before pushing or you will keep getting the same error – JHRS Mar 30 '18 at 02:03

3 Answers3

3

The database url is to hook/connect your db with Heroku. Try this approach.

if 'DATABASE_URL' in os.environ:
    DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
    }
else:
    print("Postgres URL not found, using sqlite instead")
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

Also, check if you added the DATABASE_URL into the config vars in Heroku settings. In addition, also check if in the config vars you added your SECRET_KEY and if you configured proper your ALLOWED_HOSTS`` as they have to be states like this in yoursettings.py``` :

SECRET_KEY = os.environ.get("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['<your-app-name>.herokuapp.com', '127.0.0.1']

And your requirement.txt have to look like this:


dj-database-url==0.5.0
Django==1.11.24
django-forms-bootstrap==3.1.0
gunicorn==20.0.4
Pillow==7.0.0
psycopg2-binary==2.8.4
pytz==2019.3
whitenoise==5.0.1
Elias Prado
  • 1,518
  • 2
  • 17
  • 32
2

Did you install the library using something like

pip install -r requirements.txt

or

pip install dj-database-url==0.4.2

or

.env/bin/pip install dj-database-url==0.4.2

?

jperelli
  • 6,988
  • 5
  • 50
  • 85
  • I used `pip install dj-database-url` from [migrating an existing django project](https://devcenter.heroku.com/articles/django-app-configuration#migrating-an-existing-django-project) – Waltham WECAN Aug 30 '17 at 15:41
0

This worked for me

from django.conf.urls import url
from django.urls import reverse_lazy
from django.contrib.auth.views import PasswordResetView, PasswordResetDoneView, PasswordResetConfirmView, PasswordResetCompleteView

urlpatterns = [
    url(
        'accounts/password_reset/',
        PasswordResetView.as_view(),
        name='password_reset'
    ),
    url(
        'accounts/password_reset_done/',
        PasswordResetDoneView.as_view(),
        name='password_reset_done'
    ),
    url(
        'accounts/password_reset_confirm/',
        PasswordResetConfirmView.as_view(),
        name='password_reset_confirm'
    ),
    url(
        'accounts/password_reset_complete/',
        PasswordResetCompleteView.as_view(),
        name='password_reset_complete'
    ),
]