Django ImportError while adding guardian module - django

Being a beginner of using Django, i am trying to add some module for the purpose of testing Django, but I've got a problem regarding the importError which I've googled for solution with no success. Below is my situation
The project is created to my PC J:\ while the python package is installed on C:. According to guardian's installation guide following code have to be added in django's backend:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # default
'guardian.backends.ObjectPermissionBackend',)
The problem's come, when I put the guardian's app under the directory of mysite, configurate the install_apps setting with 'guardian' and start syncdb, the error claim as below which i believe that it is because the django's filw do not understand what is "guradian" really is
File "J:\mysite\guardian\conf\settings.py", line 6, in
raise ImproperlyConfigured("In order to use django-guardian's "
django.core.exceptions.ImproperlyConfigured: In order to use django-guardian's O
bjectPermissionBackend authorization backend you have to configure ANONYMOUS_USE
R_ID at your settings module
So, I move the 'guardian' folder under Django's contrib folder, adding the sys path and configurate the install_app setting with 'django.contrib.guardian'. However, I end up with the importerror.

As it seems from the error message, you need to add the user id for anonymous user for you site.
Create a user (named maybe anonymous) and put the id of user in the settings.py file.
Obtain the user id from the database using the shell.
Put the id in the settings file:
ANONYMOUS_USER_ID = <Your anonymous USER_ID>
EDIT:
Just looked through the documentation of the django-guardian app. It also specifies this:
http://packages.python.org/django-guardian/configuration.html

Related

Google App Engine Interactive Console: ImportError: No module named models

I have a django project running on my local server under the google app engine python SDK 1.9.7.
I want to run a query in the interactive console. I have a model called Flatpage
I put the seemingly appropriate handler in the app.yaml file, namely:
url: /admin/.*
script: google.appengine.ext.admin.application
login: admin
From the docs i understood that when the local development server is running the interactive console on the admin server would be running in the same environment as the a project. Yet:
the interactive console does not recognize any of my model classes and when i try to import models I get an error: ImportError: No module named models
I have tried adding the local path to the models.py file to the directory as well as various names like app.models, appname.models and projectname.models to no avail.
However. when I copy and paste the models file into the interactive console it works.
Would someone explain how to import the models file into the interactive console,
and, secondly, why the interactive console, which seemingly should already have the models defined when the server starts, needs to have the models defined again, anyway, thanks!
I'm not sure why you're adding that handler to use the console locally, but what I do is use the Development Console (available since last year), which by default runs on port 8000, from there (localhost:8000/console most likely) you can import modules from your project.
For example if you have models.py in the same directory as app.yaml, you should be able to do:
import models
entity = models.Flatpage()
with no problem.

ImportError when using Haystack 2.0.0 with Django 1.5 and Gunicorn WSGI

I use django-haystack 2.0.0 to index my site, and it has been working great until I upgraded to Django 1.5 and started using the WSGI interface. If I just use the django_gunicorn command it works great, but the Django documentation "highly recommends" I use the gunicorn command.
When I start my site with the gunicorn command, Haystack throws the following error on any page load:
ImportError: cannot import name signals
I have no problems importing signals from the Django or Python shells. I use virtualenv and install all packages locally inside that environment. My wsgi.py file looks just like the default one in the django admin, except that I add the local path to the python path as such:
path = os.sep.join(os.path.abspath(__file__).split(os.sep)[:-2])
if path not in sys.path:
sys.path.append(path)`
Any help you could provide would be very appreciated, thank you!
I don't use gunicorn, but I had the same problem when I used the HAYSTACK_SIGNAL_PROCESSOR setting to point to a custom class that I wrote. That class imported one of my models, which eventually propagated up the import chain, to import my settings module, thus causing a circular import.
When using a setting such as HAYSTACK_SIGNAL_PROCESSOR that points to a class, make sure that class standsalone, and doesn't import either directly or indirectly the Django settings file.

Django, boto, S3 and easy_thumbnails not working in production environment

I'm using Django, django-storages with S3 (boto) in combination with easy-thumbnails. On my local machine, everything works as expected: if the thumbnail doesn't exists, it gets created and upload to S3 and saves in the easy-thumbnails database tables. But the problem is, when I push the code to my production server, it doesn't work, easy-thumbnails output an empty image SRC.
What I already noticed is that when I create the thumbnails on my local machine, the easy-thumbnail path uses backward slashes and my Linux server needs forwards slashes. If I change the slashes in the database, the thumbnails are showed on my Linux machine, but it is still not able to generate thumbnails on the Linux (production) machine.
The simple django-storages test fails:
>>> import django
>>> from django.core.files.storage import default_storage
>>> file = default_storage.open('storage_test', 'w')
Output:
django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_FILE_STORAGE, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
If I do:
>>> from base.settings import staging
>>> from django.conf import settings
>>> settings.configure(staging)
This works (I have a settings directory with 4 settings files: base.py, staging.py, development.py and production.py)
It seems that on my production server, the config file isn't loaded properly (however the rest of the website works fine). If I add THUMBNAIL_DEBUG = True to my settings file, but easy-thumbnails' debugging still doesn't work (it does work on my local machine).
What can be te problem? I've been debugging for 10+ hours already.
Try refactoring your settings to use a more object-oriented structure. A good example is outlined by [David Cramer from Disqus:
http://justcramer.com/2011/01/13/settings-in-django/
You'll put any server-specific settings in a local_settings.py file, and you can store a stripped-down version as example_local_settings.py within your repository.
You can still use separate settings files if you have a lot of settings specific to a staging or review server, but you wouldn't want to store complete database credentials in a code repo, so you'll have to customize the local_settings.py anyways. You can define which settings to include by adding imports at the top of local_settings.py:
from project.conf.settings.dev import *
Then, you can set your DJANGO_SETTINGS_MODULE to always point to the same place. This would be instead of calling settings.configure() as outlined in the Django docs:
https://docs.djangoproject.com/en/dev/topics/settings/#either-configure-or-django-settings-module-is-required
And that way, you know that your settings on your production server will definitely be imported, since local_settings.py is always imported.
first try to use:
python manage.py shell --settings=settings/staging
to load shell with correct settings file and then try to debug
For some reason, S3 and easy thumbnails in the templating language didn't seem to get along with each other ... some path problem which probably could be solved at some point.
My solution (read: workaround) was to move the thumbnail generation into the model inside the image field itseld, for example:
avatar = ThumbnailerImageField(upload_to = avatar_file_name, resize_source=dict(size=(125, 125), crop="smart"), blank = True)
For the sake of completeness:
def avatar_file_name(instance, filename):
path = "%s/avatar.%s" % (str(instance.user.username), filename.split('.')[1])
if os.path.exists(settings.MEDIA_ROOT + path):
os.remove(settings.MEDIA_ROOT + path)
return path

syncing sqlite3 for Django tutorial

I'm starting the Django tutorials and have virtual environments and django installed. I'm working on a Mac 10.6.8, which has sqlite3 already installed.
I'm working inside a virtual environment. I changed the settings.py file to:
ENGINE -- 'django.db.backends.sqlite3'
I left the NAME -- in the settings.py file blank (i.e. '') because the tutorial said
"If the file doesn't exist, it will automatically be created when you synchronize the database for the first time."
But when I run python manage.py syncdb, I get the following error:
django.core.exceptions.ImproperlyConfigured: Please fill out the database NAME in the settings module before using the database.
Why isn't my file automatically created? Or what do I need to do to solve this problem?
Thank you!
It will be created for you, but you have to specify what name to call it so just set NAME to sqlite.db or something similar. You can also specify a path so that it doesn't clog up your project directory. I usually save the dev database outside of my project.

Setting up Django on Server with WSGI getting Import Error "Module named myproject.urls Not found"

So I have created a django site and I wanted to move it from my computer to my server. I set up Django on the server, and used the WSGI configuration. When I try to go to the home page I get an Import Error, It says that the module "myproject.urls" isn't found. It's a Django error, and it looks like it is getting the settings.py file and looking at the setting for ROOT_URLCONF and seeing the right urls file. I created this project with the usual django-admin.py startproject myproject and I just wanted to see if everything was configured correctly, but now I'm getting this error.
Any Suggestions?
Remove the "myproject" from "myproject.urls". Somehow WSGI addresses the settings as the root, so no need to refer to it again.
It sounds like myproject isn't on your path - what happens if you load up a python shell and run import myproject? If that works, what happens when you run import myproject.urls? If only the second import fails, there's a syntax error in your urls.py or one of the files it imports.
#Afrowave, you saved a huge headache - thank you from me too!
Further to this - I did a little more digging and wanted to avoid having to amend a dozen files in my app to account for loosing 'myproject.' and the start of every import.
Instead, I found if you do something like this -- you don't have to :)
ROOT = '/home/user/path_to_project_root' # In my case, also the dir that contains media, templates etc
APP_ROOT = '/home/user/path_to_django_project'
sys.path.append(ROOT)
sys.path.append(APP_ROOT)
os.environ['DJANGO_SETTINGS_MODULE'] = 'django_project.settings'
Hope this helps someone in future.