Hey Stackers! :)
My Django project's directory hierarchy looks like this:
+ pybsd
|---+ devices
|---+ templates
|---+ views
|---+ interaction
|---- __init__.py
|---- geraete.py
|---- geraetemodelle.py
|---- geraetegruppen.py
|---- __init__.py
|---- ajax.py
|---- html.py
|---- misc.py
|---- __init__.py
|---- urls.py
|---- __init__.py
|---- urls.py
(Please excuse the German names. I preferred not to replace them here since it would add yet another possible error source when trying out the solutions you'll hopefully suggest and answering your questions.)
Every request to http://URL/devices/.* is dispatched to the urls.py file living in /devices:
# ...
from views import html, ajax, misc, interaction
urlpatterns = patterns('',
# ...
(r'^ajax/update/(?P<table>[a-z_]+)$', ajax.update),
(r'^ajax/delete/(?P<table>[a-z_]+)$', ajax.delete),
(r'^ajax/select_options/(?P<table>[a-z_]+)$', ajax.select_options),
(r'^interaction/geraete/info/(?P<geraet>\d+)$', interaction.geraete.info),
(r'^interaction/geraete/delete/(?P<geraet>\d+)?$', interaction.geraete.delete),
(r'^interaction/geraetemodelle/delete/(?P<geraetemodell>\d+)?$', interaction.geraetemodelle.delete),
(r'^interaction/geraetegruppen/delete/(?P<geraetegruppe>\d+)?$', interaction.geraetegruppen.delete),
# ...
)
All URL definitions work except for those referencing the interaction package. I'm constantly getting the following error:
File "/home/simon/projekte/pybsd/../pybsd/devices/urls.py", line 33, in `<module>`
(r'^interaction/geraete/info/(?P<geraet>\d+)$', interaction.geraete.info),
AttributeError: 'module' object has no attribute 'geraete'
I double-checked that the __init__.py
files don't contain anything.
Maybe you've already found the (Python- or Django-related?) mistake I made and am apparently unable to see. If not, read on. In any case, thanks for reading this long post!
Isolating the problem
1st test
It works if I provide the view functions as strings:
(r'^interaction/geraete/info/(?P<geraet>\d+)$', 'devices.views.interaction.geraete.info'),
(r'^interaction/geraete/delete/(?P<geraet>\d+)?$', 'devices.views.interaction.geraete.delete'),
(r'^interaction/geraetemodelle/delete/(?P<geraetemodell>\d+)?$', 'devices.views.interaction.geraetemodelle.delete'),
(r'^interaction/geraetegruppen/delete/(?P<geraetegruppe>\d+)?$', 'devices.views.interaction.geraetegruppen.delete'),
... or add yet another line to the imports:
from views.interaction import geraete, geraetemodelle, geraetegruppen
Using from views.interaction import *
, however, doesn't work either and results in the same error message.
2nd test
I created a file test.py in /devices:
from views import interaction
print dir(interaction)
Output:
simon@bsd-simon:~/projekte/pybsd/devices$ python test.py
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
Again, no sign of the modules I created in the interaction package (geraete.py, geraetemodelle.py, geraetegruppen.py).
Unlike in urls.py, trying from view.interaction import geraete, geraetegruppen, geraetemodelle
in test.py results in ImportError: No module named view.interaction
this time.
3rd test
I started the Django shell:
$ python manage.py shell
>>> import devices.views.interaction.geraete
>>> dir(devices.views.interaction.geraete)
['Abteilung', 'Auftrag', 'Auftragsvorlage', 'Geraet', 'Geraetegruppe', 'Geraetemodell', 'HttpResponse', 'HttpResponseBadRequest', 'HttpResponseRedirect', 'Raum', 'Standort', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'delete', 'info', 'models', 'move', 'render_to_response']
>>>
$ python manage.py shell
>>> from devices.views.interaction import geraete
>>> dir(geraete)
['Abteilung', 'Auftrag', 'Auftragsvorlage', 'Geraet', 'Geraetegruppe', 'Geraetemodell', 'HttpResponse', 'HttpResponseBadRequest', 'HttpResponseRedirect', 'Raum', 'Standort', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'delete', 'info', 'models', 'move', 'render_to_response']
>>>
$ python manage.py shell
>>> import devices.views.interaction
>>> devices.views.interaction.geraete
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'module' object has no attribute 'geraete'
>>> dir(devices.views.interaction)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']