Question

I'm just working through the DjangoBook Chapter7 tutorial on creating forms.

My issue is that I can only import one views.py file from either my books or contact directories. As a result I am only able to see the pages created by whichever views.py file is imported at the time.

I believe I need someway of differentiating between the two directories so that Django does not get confused (due to my likely bad implementation). I have also included an image of my project directory which might be useful to understand the problem.

Contact import working

from mysite.views import hello, current_datetime, hours_ahead, display_meta
from contact import views
#from books import views

urlpatterns = patterns('',
    ...

    #url(r'^search-form/$', views.search_form),
    #url(r'^search/$', views.search),    

    url(r'^contact_form/$', views.contact), 
)

Books import working

from mysite.views import hello, current_datetime, hours_ahead, display_meta
#from contact import views
from books import views

urlpatterns = patterns('',
    ....

    url(r'^search-form/$', views.search_form),
    url(r'^search/$', views.search),    

   #url(r'^contact_form/$', views.contact), 
)

My Project structure. I am working in Eclipse with Pydev.

enter image description here

Both contact and books import implemented give the below error

AttributeError at /search/

'module' object has no attribute 'search_form'

Any help is as always much appreciated.

Was it helpful?

Solution

you can use an as statement:

from contact import views as contact_views
from books import views as books_views

and the call view:

url(r'^search-form/$', books_views.search_form),
url(r'^search/$', books_views.search),
url(r'^contact_form/$', contact_views.contact), 

OTHER TIPS

You should put the urls.py in your application, as detailed in part 3 of the tutorial.

In your application directory create a urls.py,

contact
   - __init__.py
   - views.py
   - models.py
   - urls.py
books
   - __init__.py
   - views.py
   - models.py
   - urls.py

In book/urls.py, add the following:

from django.conf.urls import patterns, url

from .views import search, search_form

urlpatterns = patterns('',
    url(r'search-form/$', search_form, name='search_form'),
)

Then in your main urls.py, add the following:

url(r'^books/', include('book.urls')),

Please go through the official tutorial as the django book website is out dated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top