Django 1.3 with Webassets 0.7: assets.py ignored? - django

I'm trying to get an existing Django project to work on my mac.
I managed to configure everything and opening the project in PyCharm.
When I run I get the error:
TemplateSyntaxError at /
Caught BundleError while rendering: 'stylesheets' not found (using staticfiles finders)
We use Django 1.3 and Webassets 0.7 (just updated from earlier versions).
I have an assets.py defined in my application folder defining the various bundles.
Any suggestions on solving this?
EDIT: Ok, a bit further ... I added my project.assest to settings and now I don't have the Bundle error. I do still have another problem:
Caught BundleError while rendering: 'styles/libs/jquery-ui-timepicker-addon.css' not found
Path look ok, collectstatic works, copies, file is in place ...
any suggestions?

A project-wide assets.py (as opposed to one in an app-directory) is no longer automatically read, you need to define such files through a ASSETS_MODULES setting now.
If you are using staticfiles, pay attention to the fact that the staticfile finders will not be used unless Django is in debug mode (settings.DEBUG=True). In production mode, webassets will assume that collectstatic has been run first.
In settings.DEBUG=True mode, the reverse is true: ONLY the Django staticfile finders will be used. You could try opening a shell (./manage.py shell) and see if the following finds your file:
from django.contrib.staticfiles import finders
finders.find('styles/libs/jquery-ui-timepicker-addon.css')
If it does, then so should webassets.

I had a similar error arise
'[BUNDLE_NAME]' not found (using staticfiles finders)
This wasn't a very helpful message, so ended up looking into ./manage.py shell and running
>>> from django.conf import settings
>>> from [ASSETS_FILE_PATH.assets] import [BUNDLE_NAME]
>>> [BUNDLE_NAME]
If this isn't there it may give you another message.

Related

Django: Unable to import models globally

I am running some python scripts which pertains with a django application.
So I manually made a folder (named scripts) that contains some python scripts but is not made using ./manage.py startapp scripts. Along with this folder there is a modelsapp django app (which contains my models) and then project folder containing settings.py and urls.py.
I run ./manage.py shell < scripts/script.py to run my script.
Now here is a sample code of my scripts.py
from modelsapp.models import *
print(Room.objects.all())
This works pretty well. Now consider a second case. I now just run ./manage.py shell and then put the following commands-
>>>from modelsapp.models import *
>>>def init():
... print(Room.objects.all())
...
>>>init()
This also works pretty well. But now when I run the above code through the file in the first case, it says NameError: name 'Room' is not defined
It looks like the model classes which I just imported aren't accessibly inside a function.
This behavior is a bit weird.
Please help. In case I missed anything to mention, do comment below.
Edit: Since this is a bit weird behavior and am not able to find any answers to it, I am just going to pass the class Room directly to the function init() as a parameter to get the work done as of now.
after you run the django shell, you can run your script with:
>>>execfile('filename.py')
if you want to run it in a normal python shell (not using python manage.py shell) you have to do:
import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project(name.settings")
django.setup()

Postgres.App + Django and proper path to libgeos_c.dylib on OSX?

I have succesfully installed Postgres.App. It works just fine if I have this in settings.py:
GEOS_LIBRARY_PATH="/Applications/Postgres.app/Contents/Versions/latest/lib/libgeos_c.dylib"
If I remove the above line and execute manage.py loaddata mydata, I get:
ImportError: Could not find the GEOS library (tried "geos_c", "GEOS"). Try setting GEOS_LIBRARY_PATH in your settings.
The question: is it possible to make things work without GEOS_LIBRARY_PATH set in settings.py? The reason is that in our team not all are using OSX + Postgres.App and the path is not the same for all developers. Django Docs mention usage of LD_LIBRARY_PATH, but having the next line exported in bash did nothing:
LD_LIBRARY_PATH="/Applications/Postgres.app/Contents/Versions/latest/lib/"
I still get the same error. Am I having a wrong syntax or is there another solution so that everyone can have individual path to the library?

Django: track down causes of DeprecationWarning

I've upgraded to Django 1.4 and now when I run my development server I get the following warning:
/home/flc/venvs/myprj/lib/python2.6/site-packages/django/views/generic/simple.py:8:
DeprecationWarning: Function-based generic views have been deprecated;
use class-based views instead.
DeprecationWarning
I have tracked down most of the causes of this and fixed them by making the following changes:
django.views.generic.simple.direct_to_template => django.views.generic.base.TemplateView
django.views.generic.simple.redirect_to => django.views.generic.base.RedirectView
etc
However, I'm still getting the warning and can't figure out what I've missed. How do I get the actual module and line in my code that is causing the DeprecationWarning?
You can use the warnings module to raise an error for DeprecationWarning.
Temporarily add the following snippet to the top of your project's urls.py:
import warnings
warnings.simplefilter('error', DeprecationWarning)
The DeprecationWarning will now raise an error, so if debug=True you'll get the familiar yellow Django error page with the full traceback.
Once you've tracked down the source of the deprecation warnings, remember to remove the snippet! Note that it may be a third party app that is causing the deprecation warnings, not your own code.
If you're new to the warnings module, you might find the page on Python module of the week to be an easier introduction than the Python docs.
You can also do this on the command line so you don't need to modify your code. For example:
python -We manage.py runserver --traceback
The official doc is here. You can use abbreviations and the e in -We stands for convert warnings to error.

Django tables-import error

i am having an import error django_tables.
Could not import ikapp.views.admin. Error was: No module named django_tables.
It looks like ikapp.views.admin requires django-tables. Unfortunately it's not possible to tell which version/fork it requires, but it's likely to be either:
https://github.com/miracle2k/django-tables
https://github.com/bradleyayers/django-tables2
django_tables requires that you actually be running within the django-"primed" python environment. You can access this by going to your project's directory and typing
python manage.py shell
Then you can import django_tables with no problems.
For further info, take a look at the following: https://docs.djangoproject.com/en/dev/ref/django-admin/

Importing settings files from settings.py gives ImportException 'DJANGO_SETTINGS_MODULE is undefined'

I'm trying to create different settings files for my development and production servers. So, in settings.py I call:
try:
from localsettings import *
except ImportError, e:
print "import error", e
pass
Then, in localsettings.py, I set the variables. However, this results in an ImportError when I run syncdb with message:
Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I can't say that I understand what the problem is - if I move the exact same settings back to the original settings.py file, everything works fine. Can anyone please tell me what obvious thing I'm doing wrong?
Make sure you're not importing something strange. I remember one time, I had the following statement:
import django.contrib.auth.backends
for use in an AUTHENTICATION_BACKENDS variable. But since that import seems to require a completely loaded settings file, it failed and I received a similar error message.