Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django on Pycharm: ImproperlyConfigured with DJANGO_SETTINGS_MODULE

I am trying to use Pycharm Community Edition to improve on my code in my Django application, but I cannot run all of my Django code that I'd like. I keep getting this traceback...

Traceback (most recent call last):
 File "C:/Users/Jaysp_000/firstSite/PROJECTone/blog_static/views.py", line 1, in <module>
   from django.views.decorators.csrf import csrf_exempt
 File "C:\Python34\lib\site-packages\django\views\decorators\csrf.py", line 3, in <module>
from django.middleware.csrf import CsrfViewMiddleware, get_token
 File "C:\Python34\lib\site-packages\django\middleware\csrf.py", line 14, in <module>
from django.utils.cache import patch_vary_headers
 File "C:\Python34\lib\site-packages\django\utils\cache.py", line 26, in <module>
from django.core.cache import caches
 File "C:\Python34\lib\site-packages\django\core\cache\__init__.py", line 34, in <module>
if DEFAULT_CACHE_ALIAS not in settings.CACHES:
 File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__
self._setup(name)
 File "C:\Python34\lib\site-packages\django\conf\__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

This error seems to involve the django.views.decortors.csrf.csrf_exempt that I imported to my views.py file. I've tried other files, and they have given me no issues. There is something in particular about this import, but I don't know what.

from django.views.decortors.csrf import csrf_exempt

@csrf_exempt
def handle_hook(request):
    from django.http import HttpResponse
    from django.core.management import call_command
    result = call_command('update_blog', verbosity = 0)
    return HttpResponse(result)

The same kind of issue shows up when I am trying to run the code on the python shell (I use 3.4) and when I import django.http.request as request. I type in handle_hook(request), and I get the same kind of error.

Im being told that I must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings, but I haven't a clue on how to do that. I've looked around and I am not certain if those methods specifically speak to my issue. Any clues?

like image 972
JellisHeRo Avatar asked Aug 03 '15 21:08

JellisHeRo


People also ask

Does Django work with PyCharm?

PyCharm supports the latest Django versions. The corresponding Python versions depend on Django.

How does PyCharm detect Django?

If you cannot print the Django version from the python console in Pycharm, go to settings>Project:project_name>project Interpreter and from the list of installed packages see the installed Django and it's version for that project. Save this answer. Show activity on this post. You can run pip freeze too.

How do I open a Django file in PyCharm?

From the main menu, choose File | New Project, or click the New Project button in the Welcome screen. New Project dialog opens. In the New Project dialog, do the following: Specify project type Django.

What is Django and PyCharm?

Django project is intended for productive web development with Django. PyCharm takes care of creating specific directory structure and files required for a Django application, and providing the correct settings.


1 Answers

Go to the Run Menu, select Edit Configurations..., then select the run configuration for you tests.

Select the environment variables button. You'll see one existing variable, which is PYTHONUNBUFFERED Under that add (for example) DJANGO_SETTINGS_MODULE=mysitename.settings

enter image description here

like image 122
Hexatonic Avatar answered Sep 18 '22 19:09

Hexatonic