Chapter 7: Form Processing

35 views
Skip to first unread message

Mikey3D

unread,
Jan 16, 2008, 2:58:52 PM1/16/08
to Django users
I have an error:

Exception Type: ImproperlyConfigured at /search/
Exception Value: Error while importing URLconf 'mysite.urls': 'tuple'
object is not callable

Here're the files:

Module: mysite.books.views
---------------------------------------

from django.db.models import Q
from django.shortcuts import render_to_response
from mysite.books.models import Book

def search(request):
query = request.GET.get('q', '')
if query:
qset = (
Q(title__icontains=query) |
Q(authors__first_name__icontains=query) |
Q(authors__last_name__icontains=query)
)
results = Book.objects.filter(qset).distinct()
else:
results = []
return render_to_response("books/search.html", {
"results": results,
"query": query
})
----------------------------------------------------------------------------

Module: mysite.url
-------------------------

from django.conf.urls.defaults import *
from mysite.views import current_datetime, hours_ahead

urlpatterns = patterns('',
(r'^time/$', current_datetime),
(r'^time/plus/(\d{1,2})/$', hours_ahead),
(r'^search/$', 'mysite.books.views.search')

# Uncomment this for admin:
(r'^admin/', include('django.contrib.admin.urls')),
)
----------------------------------------------------------------------------

What's the solution?

Thanks, Mikey3D

FrankW

unread,
Jan 16, 2008, 3:40:10 PM1/16/08
to Django users
remove the quotes around mysite.books.views.search in:
(r'^search/$', 'mysite.books.views.search')

Rajesh Dhawan

unread,
Jan 16, 2008, 3:41:06 PM1/16/08
to Django users
Hi,

> urlpatterns = patterns('',
>     (r'^time/$', current_datetime),
>     (r'^time/plus/(\d{1,2})/$', hours_ahead),
>     (r'^search/$', 'mysite.books.views.search')

You are missing a comma at the end of the above line

-Rajesh D

Mikey3D

unread,
Jan 16, 2008, 4:57:39 PM1/16/08
to Django users
FrankW, I have tried remove the quotes but it didn't work.

Rajesh Dhawan, I have just add comma, it works. Thanks Rajesh but I
get an error:

---------------------------------------------------------------------
Exception Type: TemplateDoesNotExist at /search/
Exception Value: books/search.html
--------------------------------------------------------------------

Windows, I have put search.html file in:

C:\djcode\mysite\books\templates\admin
C:\djcode\mysite\books\templates
C:\djcode\mysite\books

None of them are working. Where should I put search.html file in?

Thanks, Mikey3D

Rajesh Dhawan

unread,
Jan 16, 2008, 5:08:55 PM1/16/08
to Django users
Hi again,

>
> Rajesh Dhawan, I have just add comma, it works. Thanks Rajesh but I
> get an error:
>
> ---------------------------------------------------------------------
> Exception Type: TemplateDoesNotExist at /search/
> Exception Value: books/search.html
> --------------------------------------------------------------------
>
> Windows, I have put search.html file in:
>
> C:\djcode\mysite\books\templates\admin
> C:\djcode\mysite\books\templates
> C:\djcode\mysite\books
>
> None of them are working. Where should I put search.html file in?

Put it in a new sub-dir called books in your application's templates
directory. In other words, put search.html here:
C:\djcode\mysite\books\templates\books

Of course, later, you should read up on how Django finds templates:
http://www.djangoproject.com/documentation/templates_python/#loading-templates

Cheers,
-Rajesh D

Mikey3D

unread,
Jan 16, 2008, 5:42:25 PM1/16/08
to Django users
Hi Rajesh,

The book chapter 7 doesn't say new sub-dir called books in your
application's templates directory. For the beginners who are reading
the Django book will get confusing.

http://www.djangobook.com/en/1.0/chapter07/

Thank for the loading templates link, I have done chapter 4 with no
problem. :-)

Rajesh, you are very helpful. Thank you!

Cheers, Mikey3D

Rajesh Dhawan

unread,
Jan 16, 2008, 6:27:23 PM1/16/08
to Django users


On Jan 16, 5:42 pm, Mikey3D <snapmi...@hotmail.com> wrote:
> Hi Rajesh,
>
> The book chapter 7 doesn't say new sub-dir called books in your
> application's templates directory. For the beginners who are reading
> the Django book will get confusing.

You might want to add a comment to an appropriate section in chapter
7. The book authors have actively called for feedback that will make
the second edition even better. http://www.djangoproject.com/weblog/2007/dec/16/book/

nwgadgetguy

unread,
Feb 25, 2008, 12:35:26 AM2/25/08
to Django users
> On Jan 16, 5:42 pm, Mikey3D <snapmi...@hotmail.com> wrote:
>
> Hi Rajesh,
> The book chapter 7 doesn't say new sub-dir called books in your
> application's templates directory. For the beginners who are reading
> the Django book will get confusing.

>On Jan 16, 3:27 pm, Rajesh Dhawan <rajesh.dha...@gmail.com> wrote:
>
> You might want to add a comment to an appropriate section in chapter
> 7. The book authors have actively called for feedback that will make
> the second edition even better.http://www.djangoproject.com/weblog/2007/dec/16/book/

Rajesh is correct about the fix; however the book's sample code
contains an error.

The book's example: return render_to_response("books/search.html", {
should have been: return render_to_response("search.html", {

If the second example is followed, then the HTML code would be under
mysite\books\templates,
NOT mysite\books\templates\books, which makes it a confusing fix (why
have another DIR [books]
under templates when templates is already under the books project?).

Malcolm Tredinnick

unread,
Feb 25, 2008, 12:45:14 AM2/25/08
to django...@googlegroups.com

On Sun, 2008-02-24 at 21:35 -0800, nwgadgetguy wrote:
[...]

> If the second example is followed, then the HTML code would be under
> mysite\books\templates,
> NOT mysite\books\templates\books, which makes it a confusing fix (why
> have another DIR [books]
> under templates when templates is already under the books project?).

It's quite common to have a book/templates/books/ hierarchy if you're
using the application loader for templates (which is the default).
Otherwise, you would only be able to have one template called
"search.html" amongst all of your installed apps. For common names like
index.html, base.html, search.html, etc, that would be asking for a
collision.

The app-based tempate loader merges all directories of the form
<app_name>/templates into a single tree node. If you have multiple
templates of the same name, you don't have any control over which one is
loaded (it's deterministic, but it's not a guaranteed stable piece of
behaviour in future versions).

Malcolm

--
If it walks out of your refrigerator, LET IT GO!!
http://www.pointy-stick.com/blog/

Reply all
Reply to author
Forward
0 new messages