Here's my solutions, for anyone who may be experiencing the same
problem.

In my url config, I have 4 different named patterns corresponding to
each of the possibilities:

url(r'^(?P<site>[a-zA-Z]+)/(?P<sort_by>[\+\-].\w+)/$',
'catalogueFilter', name="catalogue_search_no_filters"),
    url(r'^(?P<site>[a-zA-Z]+)/(?P<sort_by>[\+\-].\w+)/(?
P<min_estimate>\d+(?=-))?-(?P<max_estimate>(?<=-)\d+)?/$',
'catalogueFilter', name="catalogue_search_estimates"),
    url(r'^(?P<site>[a-zA-Z]+)/(?P<sort_by>[\+\-].\w+)/(?
P<description>[a-zA-Z0-9%]+)/$', 'catalogueFilter',
name="catalogue_search_desc"),
    url(r'^(?P<site>[a-zA-Z]+)/(?P<sort_by>[\+\-].\w+)/(?
P<min_estimate>\d+(?=-))?-(?P<max_estimate>(?<=-)\d+)?/(?
P<description>[a-zA-Z0-9%]+)/$', 'catalogueFilter',
name="catalogue_search_estimates_desc"),

I apologise for them being a bit messy, but you can see that each
pattern starts with "(?P<site>[a-zA-Z]+)/(?P<sort_by>[\+\-].\w+)"
These correspond to the drop down boxes in my form. They will always
be submitted. The first pattern contains just these parameters, and so
the url is called "catalogue_search_no_filters".

Second, we have a pattern for if some estimates are submitted. These
are optional form fields that can be filled in if the user wants to
narrow down the search. In order to allow for this flexibility, there
are a lot of '?' in the regular expression. The regular expression
adds a '-' character in to represent a range of values.

We then have another url for just the optional description field. This
is a simple text search.

Finally I have a named url for the optional estimate fields and an
optional description field. I was trying to write one single url that
allowed for both, but I couldn't. Instead I came up with this
solution. I different named url that points to the same view function.

The view function itself is pretty straight forward. It accepts all
the parameters and includes so default values:

def catalogueFilter(request, site='any', min_estimate=None,
max_estimate=None,
    sort_by=LotSearchForm.SORT_CHOICES_URLS[0][1], description=None):

    .... do stuff, get "results' together ...

    return object_list(
        request=request,
        queryset=results,
        page=page,
        paginate_by=20,
        template_object_name="lot",
        extra_context=dict(
            search_form=form
        )
    )

I'm using a generic view to return the HttpResponse.

The messy bit is how I determine which named url to call. I'm afriad
it's a lot of if/else statements branching off to different
HttpResponseRedirects:

if ("min_estimate" in params) or ("max_estimate" in params):
    if "description" in params:
        return HttpResponseRedirect(
            reverse('catalogue_search_estimates_desc', kwargs=params))
    else:
        return HttpResponseRedirect(
            reverse('catalogue_search_estimates', kwargs=params)
else:
    if "description" in params:
        return HttpResponseRedirect(
            reverse('catalogue_search_desc', kwargs=params))
    else:
        return HttpResponseRedirect(
            reverse('catalogue_search_no_filters', kwargs=params))

I admit this isn't elegant at all, but it works. If anyone has any
ideas on how I could improve this, please let me know. If nothing
else, it might clear up what I'm trying to do here.

Thanks,

RM

On Nov 4, 5:48 pm, redmonkey <[EMAIL PROTECTED]> wrote:
> Hi Brian,
>
> Thanks for your comment. I completely understand what you are saying,
> and anyone trying to match the "?" character should take note, but in
> this situation I am using it to try to tell Django that that named
> group in the regular expression may, or may not, be included.
>
> RM
>
> On Nov 4, 5:33 pm, Brian Neal <[EMAIL PROTECTED]> wrote:
>
> > On Nov 4, 10:50 am, redmonkey <[EMAIL PROTECTED]> wrote:
>
> > > My problem is with the URL writing. I first wrote some unit tests to
> > > find the regular expressions that worked but Django doesn't seem to
> > > like the '?' in URL configurations.
>
> > ? is a special character in regular expressions, just like $, ^, etc.
> > You must escape it if you want to use it literally, e.g. \?. However,
> > I don't think the ? as part of any GET arguments in a URL are going to
> > be available to you, from what I remember about django works.
>
> > I'm not really following what you are trying to do however.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to