recieving GET from generic views - arrrrgh

6 views
Skip to first unread message

SoCow

unread,
Apr 19, 2009, 2:02:28 PM4/19/09
to Django users
Hi all,
thanks for reading.

I've implemented search for my site following the example in the
http://www.djangobook.com/en/2.0/chapter07/. My search view receives
the search term from a GET event.

I want to use this same method in a generic view, but don't know how
Any ideas?

Many thanks

SoCow

unread,
Apr 19, 2009, 6:01:58 PM4/19/09
to Django users
ok, i'm getting there. i figured out that i need to wrap the generic
view in my own function. To make the problem clear i'm trying to get a
simple search to live in the base.html template of a blog which uses
the date_based generic view. My wrapper function consists of:

def searchwrap(request):
error = False
if 'q' in request.GET:
q = request.GET['q']
if not q:
error = True
else:
set= Mymodel.objects.filter(title__icontains=q)
return django.views.generic.date_based.archive_index(
request,
queryset = set,
template_name = 'search_results.html',
extra_context = {'set': set}
)

return date_based.archive_index(
request,
template_name = 'search_form.html',
extra_context = {
'error': error,
'queryset': Mymodel.objects.filter(status=1),
'date_field': 'pub_date'
}
)

I'm now getting the error:

archive_index() takes at least 3 non-keyword arguments (1 given)

i thought that i was passing it 3 non-keyword arguments, no?

anyone any experience with this?

Thanks


On 19 Apr, 19:02, SoCow <vivid.des...@gmail.com> wrote:
> Hi all,
> thanks for reading.
>
> I've implemented search for my site following the example in thehttp://www.djangobook.com/en/2.0/chapter07/. My search view receives

Greg

unread,
Apr 19, 2009, 11:14:01 PM4/19/09
to Django users
> return django.views.generic.date_based.archive_index(
> request,
> queryset = set,
> template_name = 'search_results.html',
> extra_context = {'set': set}
> )
...
> I'm now getting the error:
>
> archive_index() takes at least 3 non-keyword arguments (1 given)

Nope, you're passing it one non-keyword argument and 3 keyword ones.

> queryset = set,

is a keyword argument, as is the template_name and extra_context.

Hope that helps...


Anatoliy

unread,
Apr 20, 2009, 12:50:05 AM4/20/09
to Django users
Try this:

return date_based.archive_index(
request,
Mymodel.objects.filter(status=1),
'date_field': 'pub_date',
template_name = 'search_form.html',
extra_context = {
'error': error,
}
)

SoCow

unread,
Apr 20, 2009, 2:35:14 PM4/20/09
to Django users
cheers again for the response. However the code i used to get the
wrapper working is given below

def searchwrap(request):
error = False
if 'q' in request.GET:
q = request.GET['q']
if not q:
error = True
else:
articles = Mymodel.objects.filter(title__icontains=q)
return django.views.generic.date_based.archive_index(
request,
queryset = set,
template_name = 'search_results.html',
extra_context = {'set': set}
)
return date_based.archive_index(
request,
Mymodel.objects.filter(status=1),
'pub_date',
template_name = 'search_form.html',
extra_context = {
'error': error,
}
)

great!

So, in order to get a (very simple) search box on each page in a
default blog app with url which goes like /year/month/day/slug/ i
wrapped archive_index, archive_year, archive_month, archive_day and
object_detail from django.views.generic import date_based with a
function that picks up the from GET. This is obviously a very thick
way of achieving this. In the interests of elegant code could i pass
the relevant generic function view name to my wrapper? I tried this by
just including a parameter in the call for the wrapper view but it
didn't work, i thought something like this would suffice, but no:

(r'^$',views.searchwrap(generic.view)),

am i way off?

Cheers,


On Apr 20, 5:50 am, Anatoliy <Anatoliy.La...@gmail.com> wrote:
> Try this:
>
> return date_based.archive_index(
>         request,
>         Mymodel.objects.filter(status=1),
>         'date_field': 'pub_date',
>         template_name = 'search_form.html',
>         extra_context = {
>                         'error': error,
>         }
>     )
>
Reply all
Reply to author
Forward
0 new messages