Quick (probably silly) question regarding Django-Filebrowser. I'm adding the lines from readthedocs in my settings.py
MEDIA_ROOT = getattr(settings, "FILEBROWSER_MEDIA_ROOT", settings.MEDIA_ROOT)
and I'm getting
NameError: name 'settings' is not defined
If I'm right, adding an import
import filebrowser.settings
Will throw a circular reference error (it comes up asking for a SECRET_KEY) if I do so. What do I do to fix this and define settings?!
Thanks!
By looking at the source it looks like you should import Django settings:
from django.conf import settings
Related
I am working on a Django project. I am working on the register page. When I try to import my register/views.py to my mysite/urls.py file I get an error message. ModuleNotFoundError: No Module named 'register'. Both files are are in the same directory.
from django.contrib import admin
from django.urls import path, include
from register import views as v
Adding full exception message
Try the following:-
from . import views
Please add a blank __init__.py file in the register folder.
Only then python will understand that register is an importable package
Edit:
After seeing the exception, it looks like a working directory issue in pycharms. Please try the fix mentioned in this link
My settings are structured like:
/settings/__init__.py
/settings/base.py
/settings/dev.py
/settings/prod.py
The constant RANDOM_VAR is set in dev.py
When I do the following in e.g. urls.py
from django.conf import settings
print(settings.RANDOM_VAR)
I get
AttributeError: 'Settings' object has no attribute 'RANDOM_VAR'
After further testing I see that all my database settings etc. are loaded from dev.py. But when I want to access my dev.py settings through from django.conf import settings it doesn't work.
I don't want to use from <your_path>.settings import dev, because this would not work on production.
Any ideas?
Your settings structure should be like this,
/settings/__init__.py
/settings/base.py
/settings/dev.py
/settings/prod.py
the you can try something like this
from <your_path>.settings import dev
print(dev.RANDOM_VAR)
Hope this helps you
I have created a custom storage backend, the file is called storages.py and is placed in an app called core:
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class S3StaticBucket(S3BotoStorage):
def __init__(self, *args, **kwargs):
kwargs['bucket_name'] = getattr(settings, 'static.mysite.com')
super(S3BotoStorage, self).__init__(*args, **kwargs)
In settings.py, I have the follwing:
STATICFILES_STORAGE = 'core.storages.S3StaticBucket'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
When I try to do python manage.py collectstatic it shows the following error:
django.core.exceptions.ImproperlyConfigured: Error importing storage module core.storages: "No module named backends.s3boto"
And when I run python manage.py shell and try to import the same:
>>>
>>> from django.conf import settings
>>> from storages.backends.s3boto import S3BotoStorage
>>>
Any idea what I'm doing wrong?
There is a namespace conflict; the storage absolute name clashes with a storage local name. It may be unintuitive, but you can import from module in itself:
// file my_module/clash.py
import clash
print clash.__file__
Now we run python shell in a dir containing a my_module:
$ python
>>> import my_module.clash
my_module.clash.py
In short, your module tries to import a backend from itself.
You need an absolute import - Trying to import module with the same name as a built-in module causes an import error.
I had this same issue, but for me it turns out that despite django-storages being installed, boto was not. A simple pip install boto fixed the error in my scenario.
I had another type of issue that can help others, I used to have another file named storages.py but I deleted that file days ago, and still getting the Exception... the thing is I didn't had deleted the file storages.pyc !
Typo error.
Change:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
TO:
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3Boto3Storage'
Views.py is not picking up anything from settings.py
views.py has import on top
from django.conf import settings
if tried to run in this file
name = settings.APP_NAME
it throws this error
AttributeError at /test/app
'_CheckLogin' object has no attribute 'APP_NAME'
You must have redefined settings elsewhere in the module.
I think we need more information. Is it possible you're overwriting your settings variable? This suggests that your settings object is a '_CheckLogin' object Try to debug with pdb. Here's a good tutorial: http://simonwillison.net/2008/May/22/debugging/
I had a small proof of concept set up on a development server on a local machine. I'm now trying to move it over to django on a production server, which I'm using webfaction for. However, now that I'm switched over to apache from the built in django server I get the following:
ViewDoesNotExist: Could not import orgDisplay.views. Error was: No module named orgDisplay.views
But when check my orgDisplay apps folder there is a view.py in it. What am I doing wrong? I've tried adding the following to my settings.py by suggestion of the django IRC room.
import sys
sys.path.append(r"/home/user/webapps/django_project/myproject/orgDisplay")
which is the path to my app.
any ideas on how to even begin to trouble shoot this?
Thanks in advance.
I assume you're using mod_wsgi (which is recommended by Django authors), not mod_python. This is the way you should use your sys.path:
django.wsgi:
import os, sys
sys.path.append(r"/home/user/webapps/django_project/myproject/")
os.environ["DJANGO_SETTINGS_MODULE"] = "settings"
sys.stdout = sys.stderr # Prevent crashes upon print
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
urls.py:
from django.conf.urls.defaults import *
urlpatterns = (
("", include("orgDisplay.urls")),
# ...
)
orgDisplay/urls.py:
import views
urlpatterns = (
(r'^some_view/$', views.some_view), # It is actually orgDisplay.views.some_view
# many more records ...
)
It is a bad idea to add project dir itself to path since you're be getting name conflicts between multiple projects.
I think you're appending the wrong directory to sys.path. I think Python is looking in the .../myproject/orgDisplay folder for the orgDisplay package. Try removing the orgDisplay from your string, like this:
import sys
sys.path.append(r"/home/user/webapps/django_project/myproject")
The other option would be to simply add myproject (or whatever your project is actually called) in the import statement.
# instead of "from orgDisplay import views"
from myproject.orgDisplay import views
Also, make sure to restart Apache after every edit.
looking at manage.py, it does it like so:
import sys
from os.path import abspath, dirname, join
from django.core.management import setup_environ
# setup the environment before we start accessing things in the settings.
setup_environ(settings_mod)
sys.path.insert(0, join(PINAX_ROOT, "apps"))
sys.path.insert(0, join(PROJECT_ROOT, "apps"))
Provided your WSGI file is in your project directory, a slightly more flexible way is this:
import os, sys
sys.path.append(os.path.dirname(__file__))
This will enable you to change your project location later without having to modify your WSGI file.