'WSGIRequest' object has no attribute 'user' in Google App Engine - python

I'm new to django and Google App Engine, and I'm having trouble with using the datastore. Every time I make a query, such as
db.GqlQuery("SELECT * FROM Listing ORDER BY date DESC LIMIT 10")
I receive the error:
'WSGIRequest' object has no attribute 'user'
This error seems to be generated in context_processors.py within the django core. Now, the advice I've found on the Internet said to comment out user-related INSTALLED_APPS and MIDDLEWARE_CLASSES, but this does not seem to help. My code looks like this:
MIDDLEWARE_CLASSES = (
# 'django.middleware.common.CommonMiddleware',
# 'django.contrib.sessions.middleware.SessionMiddleware',
# 'django.contrib.auth.middleware.AuthenticationMiddleware',
# 'django.middleware.doc.XViewMiddleware',
)
INSTALLED_APPS = (
# 'django.contrib.auth',
'django.contrib.contenttypes',
# 'django.contrib.sessions',
'django.contrib.sites',
)
My Listing object is defined as the following (it had a author property earlier, but this is now commented out and the object was redefined with a new name):
class Listing(db.Model):
#author = db.UserProperty()
address = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
coords = db.GeoPtProperty()
Does anyone know what is causing this error, and how to fix it? Is it perhaps a case of having to reset the settings somehow?

UPDATE
The solution suggested by sdolan seems to be to add the following to the settings.py of the app:
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.debug",
"django.core.context_processors.i18n")
This effectively removes the third default processor, django.core.context_processors.auth (which shouldn't be there because for AppEngine we don't want Django's auth component).
Thank you, sdolan, for the solution! hopefully someone else can use it, too. :)
#Nick, I think it's worth putting this golden piece about CONTEXT_PROCESSORS in the tutorial (http://code.google.com/appengine/articles/django.html)
(Original followup to the question)
Have the same problem, looking for solution.... All works fine when settings.py contains
DEBUG = True
but this error pops up (and kills my motivation to proceed with learning) when I switch to
DEBUG = False
#Nick Johnson, here's the stack trace:
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3211, in _HandleRequest
self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 3154, in _Dispatch
base_env_dict=env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 527, in Dispatch
base_env_dict=base_env_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2404, in Dispatch
self._module_dict)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2314, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver.py", line 2212, in ExecuteOrImportScript
script_module.main()
File "C:\Dev\appengine\djangotest\main.py", line 37, in main
util.run_wsgi_app(application)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\util.py", line 97, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\webapp\util.py", line 115, in run_bare_wsgi_app
result = application(env, _start_response)
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\core\handlers\wsgi.py", line 189, in __call__
response = self.get_response(request)
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\core\handlers\base.py", line 103, in get_response
return callback(request, **param_dict)
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\views\defaults.py", line 79, in page_not_found
return http.HttpResponseNotFound(t.render(RequestContext(request, {'request_path': request.path})))
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\template\context.py", line 100, in __init__
self.update(processor(request))
File "C:\Program Files (x86)\Google\google_appengine\lib\django\django\core\context_processors.py", line 18, in auth
'user': request.user,
AttributeError: 'WSGIRequest' object has no attribute 'user'

Related

Django NoModuleFoundError occurs only when adding a valid path to urls.py of my project folder

I have set up a Django project according to Victor Freitas' excellent post on production ready Django boilerplate here https://simpleisbetterthancomplex.com/tutorial/2021/06/27/how-to-start-a-production-ready-django-project.html
It took me a day to refactor my whole project with 7 apps to fit into that boilerplate. All was fine and with everything working until I started developing my urls paths and templates.
For some reason when adding a url path to main urls.py file within the main project folder Django fires me a NoModuleFoundError stating 'ModuleNotFoundError: No module named 'categories' . Categories is the name of my app and it is properly installed in base.py config file. Code below:
# SIGP3A/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
# path('categories/', include('categories.urls')), # <-- THIS LINE RIGHT HERE
]
If I uncomment the above pointed line I get the error message. If I comment it out it passes Django checks.
See bellow bits and pieces of the code that I believe are relevant to the question:
# SIGP3A/Comps/categories/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.categories, name='categories'),
]
See below my installed apps where categories is included. Note Comps is INSIDE main project folder and it is where I keep all my apps.
I am using local.py config which imports from base.py. Like I said, everything works BEFORE I start adding url paths.
I commented out everything related to django debug toolbar just to filter out a few things too while debuging it.
See below how categories is registered in apps.py:
from django.apps import AppConfig
class CategoriesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'SIGP3A.Comps.categories'
I spent my morning investigating this with trials and errors only to find differing errors that led me even farther from a solution. Feeling at the point I don't know what I am doing any longer. I wish to follow this boilerplate that makes total sense to me, but I'm stuck thinking I should go back to my simpler, although not production-ready approach.
Here is the whole traceback:
(.venv) PS D:\HDD_Code\SIGP3A_chest\SIGP3A> py manage.py check
Traceback (most recent call last):
File "D:\HDD_Code\SIGP3A_chest\SIGP3A\manage.py", line 22, in <module>
main()
File "D:\HDD_Code\SIGP3A_chest\SIGP3A\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
utility.execute()
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\base.py", line 460, in execute
output = self.handle(*args, **options)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\commands\check.py", line 76, in handle
self.check(
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\management\base.py", line 487, in check
all_issues = checks.run_checks(
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\checks\registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\checks\urls.py", line 14, in check_url_config
return check_resolver(resolver)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\core\checks\urls.py", line 24, in check_resolver
return check_method()
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\urls\resolvers.py", line 480, in check
for pattern in self.url_patterns:
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\utils\functional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\urls\resolvers.py", line 696, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\utils\functional.py", line 49, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "D:\HDD_Code\SIGP3A_chest\.venv\lib\site-packages\django\urls\resolvers.py", line 689, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\fsoar\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
ModuleNotFoundError: No module named 'categories'
And here is the whole list of requirements in my virtual environment. For this job I decided to not create separated environments as it is experimental at this point.
asgiref==3.5.2
asttokens==2.0.5
backcall==0.2.0
black==22.6.0
certifi==2022.6.15
click==8.1.3
colorama==0.4.5
coverage==6.4.2
decorator==5.1.1
distlib==0.3.5
dj-database-url==1.0.0
Django==4.0.6
django-debug-toolbar==3.5.0
executing==0.9.1
factory-boy==3.2.1
Faker==13.15.1
filelock==3.7.1
flake8==5.0.3
gunicorn==20.1.0
ipython==8.4.0
isort==5.10.1
jedi==0.18.1
matplotlib-inline==0.1.3
mccabe==0.7.0
mypy-extensions==0.4.3
packaging==21.3
parso==0.8.3
pathspec==0.9.0
pickleshare==0.7.5
platformdirs==2.5.2
pluggy==1.0.0
prompt-toolkit==3.0.30
psycopg2==2.9.3
pure-eval==0.2.2
py==1.11.0
pycodestyle==2.9.0
pyflakes==2.5.0
Pygments==2.12.0
pyparsing==3.0.9
python-dateutil==2.8.2
python-decouple==3.6
pytz==2022.1
sentry-sdk==1.9.0
six==1.16.0
sqlparse==0.4.2
stack-data==0.3.0
toml==0.10.2
tomli==2.0.1
tox==3.25.1
traitlets==5.3.0
tzdata==2022.1
urllib3==1.26.11
virtualenv==20.16.2
wcwidth==0.2.5
I think I need to import something somewhere but I don't know exactly what, given that categories is already registered.
You need to use the actual app name for Django to know where the urls.py file to include is:
# your installed app name
INSTALLED_APPS = [
...
'SIGP3A.Comps.categories'
]
# your urls.py file
path('categories/', include('SIGP3A.Comps.categories.urls'))

Detecting and correcting Circular Imports

So, this is an extension of what I asked last week (Why do I need to import a class within this class, instead of at the top of the file?) but after extensive debugging, I feel like it might warrant a new question.
I have a number of modules, all with imports. I suspect there may be a circular import but I haven't been able to find it.
Here's the weird thing:
In one models.py file, I have:
from django.db import models
from django.core.exceptions import ObjectDoesNotExist
from heythere.models import Notification as HeyThere
from heythere.models import NotificationManager as HeyThereManager
from django.contrib.auth.models import User
then some model definitions, including one with a method:
def to_user(self):
try:
return User.objects.get(username=str(self.username))
except ObjectDoesNotExist:
return None
and a little further down:
from ownership.apps.Assets.models import Item
When it's like this, I get AttributeError: 'NoneType' object has no attribute 'objects' (referring to User). So it's set to None, somehow, as opposed to raising a global name XXX is not defined.
BUT THEN, I tried accessing the User right after the import:
me = User.objects.get(username='xxxx')
print me
It printed correctly upon running runserver, so I know it can access User there, but then gave me the error message ImportError: cannot import name Item. How did simply accessing the model cause an import error? (FWIW, Item does have a number of FK relations to User)
What gives?
Traceback when User.objects.get() is included:
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/utils/autoreload.py", line 93, in wrapper
fn(*args, **kwargs)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 101, in inner_run
self.validate(display_num_errors=True)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/management/base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/management/validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/db/models/loading.py", line 196, in get_app_errors
self._populate()
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/db/models/loading.py", line 78, in _populate
self.load_app(app_name)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/db/models/loading.py", line 99, in load_app
models = import_module('%s.models' % app_name)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/utils/importlib.py", line 40, in import_module
__import__(name)
File "/Users/xxxxx/code/ownership/ownership/apps/Assets/models.py", line 12, in <module>
import permissions
File "/Users/xxxxx/code/ownership/ownership/apps/Assets/permissions.py", line 4, in <module>
from ownership.apps.Shared.models import Person
File "/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py", line 87, in <module>
from ownership.apps.Assets.models import Item
ImportError: cannot import name Item
And without (how the code should work):
Environment:
Request Method: POST
Request URL: http://localhost:8000/application/new
Django Version: 1.6.1
Python Version: 2.7.2
Installed Applications:
('suit',
'south',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.redirects',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'rest_framework',
'ldap_sync',
'crispy_forms',
'ownership.apps.Catalog',
'ownership.apps.Assets',
'ownership.apps.Shared',
'ownership.libs.display',
'django_tables2',
'haystack',
'autocomplete_light',
'reversion',
'heythere',
'polymorphic',
'django_extensions',
'debug_toolbar')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.redirects.middleware.RedirectFallbackMiddleware',
'django.middleware.transaction.TransactionMiddleware',
'reversion.middleware.RevisionMiddleware',
'ownership.libs.shibboleth.CustomHeaderMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware')
Traceback:
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
114. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xxxxx/code/ownership/ownership/apps/Assets/crud_views.py" in connect_create_view
48. return subview.as_view()(request, model_name=model_name)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/views/generic/base.py" in view
69. return self.dispatch(request, *args, **kwargs)
File "/Users/xxxxx/.virtualenvs/ownership/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
87. return handler(request, *args, **kwargs)
File "/Users/xxxxx/code/ownership/ownership/apps/Assets/crud_views.py" in post
280. create_new_asset_notification(request, new_item)
File "/Users/xxxxx/code/ownership/ownership/apps/Shared/views.py" in create_new_asset_notification
123. send_email(request, subject, body, owner.to_user().email, )
File "/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py" in to_user
52. return User.objects.get(username=str(self.username))
Exception Type: AttributeError at /application/new
Exception Value: 'NoneType' object has no attribute 'objects'
Note this in my stack trace:
File "/Users/xxxxx/code/ownership/ownership/apps/Assets/models.py", line 12, in <module>
import permissions File "/Users/xxxxx/code/ownership/ownership/apps/Assets/permissions.py", line 4, in <module>
from ownership.apps.Shared.models import Person File "/Users/xxxxx/code/ownership/ownership/apps/Shared/models.py", line 87, in <module>
from ownership.apps.Assets.models import Item
Assets/models.py was importing permissions
Permissions was importing Shared.models.Person
Shared/models was importing Assets.models.Item
which was a circular import.
This was corrected by moving from django.contrib.auth.models import User to inside of the Person model. I don't quite understand why--if anyone wants to elaborate and provide a better explanation before I accept my own answer, please do.

Django static path is not properly set

I am trying to learn Django,
In settings.py I have set:
MEDIA_ROOT = '/home/hussain/django/ratingsite/ratingsite/media'
MEDIA_URL = 'media/'
STATIC_ROOT = '/home/hussain/django/ratingsite/ratingsite/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
'/home/hussain/django/ratingsite/ratingsite/static',)
I have my css files in my
/home/hussain/django/ratingsite/ratingsite/static/css/default.css
but when I try to access the webpage it doesnot load the css and gives Error.
"NetworkError: 500 Internal Server Error - http://localhost:8000/static/css/default.css"
I am new to this so I dont understand what is my root directory. what runs first and how Django tries to find the resources. If someone can guide me through this it would be great.
Guyz Guyz I was just doing hit and try and this happened. I renamed my static folder to staticfiles and then in STATICFILES_DIRS replaced the path from '/home/hussain/django/ratingsite/ratingsite/static/', to '/home/hussain/django/ratingsite/ratingsite/staticfiles/', and it worked. I have No Idea why. can someone please explain ?
output of python manage.py runserver --traceback
hussain#jarvis:~/django/ratingsite$ python manage.py runserver --traceback
Validating models...
0 errors found
December 08, 2013 - 01:48:39
Django version 1.5.4, using settings 'ratingsite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/Dec/2013 01:48:41] "GET / HTTP/1.1" 200 930
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
return super(StaticFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
return self.serve(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
return serve(request, self.file_path(request.path), insecure=True)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
absolute_path = finders.find(normalized_path)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
for finder in get_finders():
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
yield get_finder(finder_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
result = func(*args)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
return Finder()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
"The STATICFILES_DIRS setting should "
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
[08/Dec/2013 01:48:42] "GET /static/css/default.css HTTP/1.1" 500 59
Traceback (most recent call last):
Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 73, in __call__
return super(StaticFilesHandler, self).__call__(environ, start_response)
return super(StaticFilesHandler, self).__call__(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 255, in __call__
response = self.get_response(request)
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 63, in get_response
return self.serve(request)
return self.serve(request)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 56, in serve
return serve(request, self.file_path(request.path), insecure=True)
return serve(request, self.file_path(request.path), insecure=True)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/views.py", line 38, in serve
absolute_path = finders.find(normalized_path)
absolute_path = finders.find(normalized_path)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 238, in find
for finder in get_finders():
for finder in get_finders():
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 253, in get_finders
yield get_finder(finder_path)
yield get_finder(finder_path)
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 31, in wrapper
result = func(*args)
result = func(*args)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 275, in _get_finder
return Finder()
return Finder()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 62, in __init__
"The STATICFILES_DIRS setting should "
"The STATICFILES_DIRS setting should "
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
This makes sense now.
The problem is this as is written in your stacktrace:
ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting
And per your initial configuration:
STATIC_ROOT = '/home/hussain/django/ratingsite/ratingsite/static'
STATICFILES_DIRS = (
'/home/hussain/django/ratingsite/ratingsite/static',)
You will not that both of these point to exactly the same directory. This is not allowed for the reason described below.
The ImproperlyConfigured error was fixed when you changed the location of one of these configurations (so they were no longer identical).
The solution here is to:
create a different STATIC_ROOT directory (we'd call this something like /public/static/) do not put any files in this directory this is where all of the static files are automatically collected to when your site is ready to publish.
put the location/s of the files that you are developing in the STATICFILES_DIRS.
When you are ready to publish your project you are able to run $ ./manage.py collectstatic which will go through all the apps and staticfiles directories your project uses and compile a set of all the "final" static files you need in the STATIC_ROOT directory ready for publication with your finished site.
If your STATIC_ROOT directory was included in the directories that was gone through by collectstatic there would obviously be a recursive loop.
Have you added the following to your urls.py?
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = patterns('',
# ... the rest of your URLconf goes here ...
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
Per the instructions here:
https://docs.djangoproject.com/en/dev/howto/static-files/
Although you are getting a 500 error which suggests another problem.
What is the output of?:
$ python manage.py validate --traceback
Does this state some other kind of error?
How about the output of:
$ python manage.py shell
>>> from django.conf import settings
>>> print(settings.STATICFILES_DIRS)
As it is returning 500 rather than the expected 404 you could run:
$ python manage.py runserver --traceback
... and then see what the traceback history returns when you load the webpage.

django-pipeline and s3boto storage don't seem to work together

I'm trying to use django-pipeline-1.1.27 with s3boto to compress and filter static files, and then upload them to an s3 bucket. If I just use:
PIPELINE_STORAGE = 'pipeline.storage.PipelineFinderStorage'
Then it works and I get a static folder with the nice versioned file that I configured. As soon as I switch to
PIPELINE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
I get
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_manager(settings)
File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/my/virtual/env/lib/python2.7/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/management/commands/synccompress.py", line 39, in handle
packager.pack_stylesheets(package, sync=sync, force=force)
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/packager.py", line 52, in pack_stylesheets
**kwargs)
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/packager.py", line 60, in pack
package['output'], package['paths'])
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/__init__.py", line 45, in need_update
version = self.version(paths)
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/__init__.py", line 20, in version
return getattr(self.versioner, 'version')(paths)
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/hash/__init__.py", line 37, in version
buf = self.concatenate(paths)
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/hash/__init__.py", line 27, in concatenate
return '\n'.join([self.read_file(path) for path in paths])
File "/my/virtual/env/lib/python2.7/site-packages/pipeline/versioning/hash/__init__.py", line 31, in read_file
file = storage.open(path, 'rb')
File "/my/virtual/env/lib/python2.7/site-packages/django/core/files/storage.py", line 33, in open
file = self._open(name, mode)
File "/my/virtual/env/lib/python2.7/site-packages/storages/backends/s3boto.py", line 177, in _open
raise IOError('File does not exist: %s' % name)
IOError: File does not exist: css/style.css
which is one of my source files. So why does pipeline no longer want to do the filter/concatenate/compress steps when I switch to s3boto storage?
It may be that I'm doing something. Here is other config in case it helps:
INSTALLED_APPS = (
...
'pipeline',
'storages',
)
STATICFILES_FINDERS = (
'pipeline.finders.PipelineFinder',
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATIC_ROOT = "/some/path/outside/django_project/deploy_static"
STATICFILES_DIRS = () # All statics in this site are in apps
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
PIPELINE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
PIPELINE = True
PIPELINE_AUTO = True
PIPELINE_VERSION = True
PIPELINE_VERSION_PLACEHOLDER = 'VERSION'
PIPELINE_VERSIONING = 'pipeline.versioning.hash.SHA1Versioning'
PIPELINE_CSS = {
'standard': {
'source_filenames': (
'css/style.css',
...
),
'output_filename': 'css/all-VERSION.css',
'extra_context': {
'media': 'screen,projection',
},
}
}
My site is on Django 1.3.1.
The command I'm running is:
python manage.py synccompress --force
The AWS creds are also in settings, but that's moot because it's not even getting to that point.
UPDATE Added full stack and settings requested in comments
UPDATE At the request of the library author, I tried upgrading to the latest beta. Observations from that so far:
I don't know how to get versioned compressed files now
collectstatic leaves me with the compressed files and the originals
Still getting the same error from django-pipeline when boto storage is configured: it wants to send my source files to s3, but I can't even see where it's staging my assets. Nothing gets placed in STATIC_ROOT.
UPDATE I've created the simplest project that works for finder storage and then breaks with S3Boto. I've pushed it to github, and included a capture of the stacktrace.
https://github.com/estebistec/simple_pipeline
https://raw.github.com/estebistec/simple_pipeline/master/STACKTRACE
I would be ecstatic if I could be told I'm doing some really dumb and this should all just work.
django-pipeline 1.1.x is a bit dumb about how you should use staticfiles, it prefers to have everything in one place.
I suggest you to try django-pipeline 1.2 with latest django-staticfiles or django 1.4.
Use a custom like this :
STATICFILES_STORAGE = 'your.app.S3PipelineStorage'
The code looks like this :
from staticfiles.storage import CachedFilesMixin
from pipeline.storage import PipelineMixin
from storages.backends.s3boto import S3BotoStorage
class S3PipelineStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage):
pass
You can find how to fix your application, but there is still a bug with compiled files unless you use version 1.2c1 : https://gist.github.com/1999564
I just experienced this same error on a Django 1.6 project with django-pipeline==1.3.23, and the solution was simply removing the PIPELINE_STORAGE setting.
There is another problem with similar error message that affects earlier and current version (1.5.4) of django-pipeline.
The error message is IOError: File does not exist, and it happens in s3boto.py.open() and packager.pack_stylesheets(). You might hit the problem if you use any of the compiler (Compass, Sass, Less etc). I suspect it would also affect JS compiler, but I have not confirmed.
In a nutshell, the compiler generates output file in the local static storage and the next steps, compress is trying to find the output in s3 storage.
If it affects you, you might want to take a look at https://github.com/cyberdelia/django-pipeline/issues/473. There are two pull-requests (patches), one made by skirsdeda and another made by thomasyip (me). Both might solve your problem. If you would like the compiled (but, pre-compressed) file to be copied to s3 and available to the app, the you would take thomasyip (me)'s patch.
Here is full Traceback for the problem:
Traceback (most recent call last):
File "apps/manage.py", line 16, in <module>
execute_from_command_line(sys.argv)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
output = self.handle(*args, **options)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
return self.handle_noargs(**options)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 171, in handle_noargs
collected = self.collect()
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 117, in collect
for original_path, processed_path, processed in processor:
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/storage.py", line 26, in post_process
packager.pack_stylesheets(package)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/packager.py", line 96, in pack_stylesheets
variant=package.variant, **kwargs)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/packager.py", line 106, in pack
content = compress(paths, **kwargs)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 73, in compress_css
css = self.concatenate_and_rewrite(paths, output_filename, variant)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 137, in concatenate_and_rewrite
content = self.read_text(path)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 220, in read_text
content = self.read_bytes(path)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/pipeline/compressors/__init__.py", line 214, in read_bytes
file = staticfiles_storage.open(path)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 35, in open
return self._open(name, mode)
File "/Users/thomas/Dev/Project/main-server/venv/lib/python2.7/site-packages/storages/backends/s3boto.py", line 366, in _open
raise IOError('File does not exist: %s' % name)
IOError: File does not exist: sheets/sass/sheets.css
Complementing the answers, you could use GZIP as well when compressing:
from django.contrib.staticfiles.storage import CachedFilesMixin
from pipeline.storage import PipelineMixin
from storages.backends.s3boto import S3BotoStorage
class S3PipelineStorage(PipelineMixin, CachedFilesMixin, S3BotoStorage):
def __init__(self, *args, **kwargs):
self.gzip = True
super(S3PipelineStorage, self).__init__(*args, **kwargs)
Using the settings as follows:
COMPRESS_STORAGE = STATICFILES_STORAGE = 'my.apps.main.S3PipelineStorage'
Not sure how this seemed to work for everyone else. I followed the solution above and kept getting the following error:
Traceback (most recent call last):
File "manage.py", line 24, in <module>
execute_from_command_line(sys.argv)
File "python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "python3.4/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "python3.4/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle
collected = self.collect()
File "python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
for original_path, processed_path, processed in processor:
File "python3.4/site-packages/pipeline/storage.py", line 26, in post_process
packager.pack_stylesheets(package)
File "python3.4/site-packages/pipeline/packager.py", line 96, in pack_stylesheets
variant=package.variant, **kwargs)
File "python3.4/site-packages/pipeline/packager.py", line 105, in pack
paths = self.compile(package.paths, force=True)
File "python3.4/site-packages/pipeline/packager.py", line 99, in compile
return self.compiler.compile(paths, force=force)
File "python3.4/site-packages/pipeline/compilers/__init__.py", line 56, in compile
return list(executor.map(_compile, paths))
File "/usr/local/lib/python3.4/concurrent/futures/_base.py", line 549, in result_iterator
yield future.result()
File "/usr/local/lib/python3.4/concurrent/futures/_base.py", line 402, in result
return self.__get_result()
File "/usr/local/lib/python3.4/concurrent/futures/_base.py", line 354, in __get_result
raise self._exception
File "/usr/local/lib/python3.4/concurrent/futures/thread.py", line 54, in run
result = self.fn(*self.args, **self.kwargs)
File "python3.4/site-packages/pipeline/compilers/__init__.py", line 42, in _compile
outdated = compiler.is_outdated(input_path, output_path)
File "python3.4/site-packages/pipeline/compilers/__init__.py", line 85, in is_outdated
return self.storage.modified_time(infile) > self.storage.modified_time(outfile)
File "python3.4/site-packages/storages/backends/s3boto.py", line 480, in modified_time
return parse_ts(entry.last_modified)
AttributeError: 'NoneType' object has no attribute 'last_modified'
It wasn't until I came across this solution that I started to find what worked for me. Here's the storage that I ended up using that saved the file locally as well as in S3 which got me passed all errors:
from django.contrib.staticfiles.storage import ManifestFilesMixin
from django.core.files.storage import get_storage_class
from pipeline.storage import PipelineMixin
from storages.backends.s3boto import S3BotoStorage
class StaticStorage(PipelineMixin, ManifestFilesMixin, S3BotoStorage):
"""Custom storage for static content."""
def __init__(self, *args, **kwargs):
super(StaticStorage, self).__init__(*args, **kwargs)
self.local_storage = get_storage_class(
'django.contrib.staticfiles.storage.StaticFilesStorage')()
def save(self, name, content):
name = super(StaticStorage, self).save(name, content)
self.local_storage._save(name, content)
return name

django exception while using Filter (ImportError: No Module named ..)

When I try using in Django the "filter" Method:
dumpData.objects.filter(bid = True)
I get the following Exception:
ImportError: No module named Trades
File "XXX\CacheUtil.py", line 141, in loadItems print DumpData.objects.filter(bid = True)
File "XXX\site-packages\django\db\models\manager.py", line 141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "X:\Python27\lib\site-packages\django\db\models\query.py", line 550, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "X:\Python27\lib\site-packages\django\db\models\query.py", line 568, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "X:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1194, in add_q
can_reuse=used_aliases, force_having=force_having)
File "X:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1069, in add_filter
negate=negate, process_extras=process_extras)
File "X:\Python27\lib\site-packages\django\db\models\sql\query.py", line 1248, in setup_joins
field, model, direct, m2m = opts.get_field_by_name(name)
File "X:\Python27\lib\site-packages\django\db\models\options.py", line 307, in get_field_by_name
cache = self.init_name_map()
File "X:\Python27\lib\site-packages\django\db\models\options.py", line 337, in init_name_map
for f, model in self.get_all_related_m2m_objects_with_model():
File "X:\Python27\lib\site-packages\django\db\models\options.py", line 414, in get_all_related_m2m_objects_with_model
cache = self._fill_related_many_to_many_cache()
File "X:\Python27\lib\site-packages\django\db\models\options.py", line 428, in _fill_related_many_to_many_cache
for klass in get_models():
File "X:\Python27\lib\site-packages\django\db\models\loading.py", line 167, in get_models
self._populate()
File "X:\Python27\lib\site-packages\django\db\models\loading.py", line 61, in _populate
self.load_app(app_name, True)
File "X:\Python27\lib\site-packages\django\db\models\loading.py", line 76, in load_app
app_module = import_module(app_name)
File "X:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
ImportError: No module named Trades
Trade is my Django App wich is in the settings.py under
INSTALLED_APPS = (
(...),
'Trades')
DumpData is defined as:
class DumpData(models.Model):
orderId = models.BigIntegerField()
typeId = models.BigIntegerField()
price = models.DecimalField(decimal_places=2,max_digits=20)
bid = models.BooleanField()
issued = models.DateField()
dateDumped = models.DateTimeField()
If I try only
dumbData.objects.all()
all works fine so it's probably not a Server/DB problem. (Also I can save the Date without an exception).
I didn't found any documentation about it, but I would suggest to use lowercase names for apps.
Also, try :
dumpData.objects.filter(bid = True).all()
Ok i solved my Problem.
The reason because it didnt worked was in the Project Strukture, i had:
\src
\PackageNr1
Main.py <-- File where Django was called.
\PackageNr2 <-- Package for the Django Files
\trades <-- Django App
models.py ,
views ...usw
settings.py <-- Django Settings File
manage.py
Witch this Struckture i could call things like "save()" or "object.all()" and it worked fine. But for most call it didnt worked. So i moved my Django Package into the Main Pakcage and now it works. The new Struckture looks like this:
\src
\PackageNr1
\trades <-- The django app
models.py
views ....
Main.py
settings.py <--Django Files
manage.py
And this works fine for me.

Categories

Resources