1

I'm implementing django-haystack for the first time and need to make a geo-spatial search. Is it possible to access user inputs from urls.py?

I followed the haystack documentation and was able to do basic search using

from django.conf.urls.defaults import *
from haystack.forms import ModelSearchForm, HighlightedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import SearchView
from haystack.views import SearchView, search_view_factory

**sqs = SearchQuerySet().all()**

urlpatterns = patterns('haystack.views',
url(r'^$', search_view_factory(
    view_class=SearchView,
    template='search/search.html',
    searchqueryset=sqs,
    form_class=HighlightedSearchForm
    ), name='haystack_search'),
)

I would need to do sqs = SearchQuerySet().dwithin('location', user_location, D(mi=5))

where user_location will be an user input and going to be passed through GET variable, is there any way I can access that here?

Thank you.

1 Answer 1

1

Check How to access HttpRequest from urls.py in Django

Then your code looks like

urlpatterns = patterns('haystack.views',
url(r'^$', lambda request, *args, **kwargs: search_view_factory(
    view_class=SearchView,
    template='search/search.html',
    searchqueryset=SearchQuerySet().dwithin('location', request.GET.get('user_location'), D(mi=5)),
    form_class=HighlightedSearchForm
    ), name='haystack_search')(request, *args, **kwargs),
)

Normally its better to explicitly write a view to invoke corresponding search view.

Sign up to request clarification or add additional context in comments.

1 Comment

Normally its better to explicitly write a view to invoke corresponding search view. - yes, do not turn urls.py into psuedo-view controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.