rq / django-rq

A simple app that provides django integration for RQ (Redis Queue)
MIT License
1.82k stars 286 forks source link

django.core.exceptions.ImproperlyConfigured: Requested setting RQ_SHOW_ADMIN_LINK, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. #619

Open SurajPysquad opened 1 year ago

SurajPysquad commented 1 year ago

i have added in setting file but it give me error this

Traceback (most recent call last): File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/rq/worker.py", line 1359, in perform_job rv = job.perform() File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/rq/job.py", line 1178, in perform self._result = self._execute() File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/rq/job.py", line 1215, in _execute result = self.func(*self.args, **self.kwargs) File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/rq/job.py", line 387, in func return import_attribute(self.func_name) File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/rq/utils.py", line 204, in import_attribute module = importlib.import_module(module_name) File "/usr/local/lib/python3.8/importlib/init.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked File "", line 848, in exec_module File "", line 219, in _call_with_frames_removed File "/home/pys/suraj_workplace/projects/captain/./captain/core/utils/email.py", line 14, in def send_template_mail(subject, template, to, context): File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/django_rq/decorators.py", line 28, in job queue = get_queue(queue) File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/django_rq/queues.py", line 150, in get_queue from .settings import QUEUES File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/django_rq/settings.py", line 8, in SHOW_ADMIN_LINK = getattr(settings, 'RQ_SHOW_ADMIN_LINK', False) File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/django/conf/init.py", line 102, in getattr self._setup(name) File "/home/pys/suraj_workplace/virtualenv/captain_env/lib/python3.8/site-packages/django/conf/init.py", line 82, in _setup raise ImproperlyConfigured( django.core.exceptions.ImproperlyConfigured: Requested setting RQ_SHOW_ADMIN_LINK, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Narsimha96 commented 7 months ago

Im facing similar issue. Any solution?

qasim29 commented 5 months ago

The error seems to arise due to the order of imports and possibly when settings are accessed during the import of modules.

I had the same issue.

just make sure wherever you guys are using: import os

make sure to load the DJANGO_SETTINGS_MODULE on the next line os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<your-folder-name>.settings')

This is especially important in entry-point scripts like asgi.py or wsgi.py.

for better clarity I have attached my asgi.py file for the order of import

import os

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.settings')

from django.core.asgi import get_asgi_application
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from users.routing import websocket_urlpatterns

application = ProtocolTypeRouter({
    "http": get_asgi_application(),
    "websocket": AuthMiddlewareStack(
        URLRouter(
            websocket_urlpatterns
        )
    ),
})