2

I'm new to Django...

I have installed a Django external App called Haystack, this external App have a "views.py" file inside "python2.6/site-packages/haystack". I think this "views.py" is called a "generic view" in Django terms.

This generic view is called using "urls.py" like this:

urlpatterns = patterns('haystack.views', 
                   url(r'^search/$', FacetedSearchView(form_class=FacetedSearchForm, searchqueryset=sqs), name='haystack_search'), 
)

I need to make the jump from this generic views to my App. My question goes in the direction of How Can I Do This?

The code of Haystack "views.py" goes like this:

from django.conf import settings
from django.core.paginator import Paginator, InvalidPage
from django.http import Http404
from django.shortcuts import render_to_response
from django.template import RequestContext
from haystack.forms import ModelSearchForm, FacetedSearchForm
from haystack.query import EmptySearchQuerySet


RESULTS_PER_PAGE = getattr(settings, 'HAYSTACK_SEARCH_RESULTS_PER_PAGE', 20)


class SearchView(object):
    ...

    def __init__(self, template=None, load_all=True, form_class=None, searchqueryset=None, context_class=RequestContext, results_per_page=None):
    ...

    def __call__(self, request):
    ...

    def build_form(self, form_kwargs=None):
    ...

    def get_query(self):
    ...

    def get_results(self):
    ...

    def build_page(self):
    ...

    def extra_context(self):
    ...

    def create_response(self):
    ...


def search_view_factory(view_class=SearchView, *args, **kwargs):
    ...


class FacetedSearchView(SearchView):
    ...

    def __init__(self, *args, **kwargs):
    ...

    def build_form(self, form_kwargs=None):
    ...

    def extra_context(self):
    ...


def basic_search(request, template='search/search.html', load_all=True, form_class=ModelSearchForm, searchqueryset=None, context_class=RequestContext, extra_context=None, results_per_page=None):
    ...

Can someone give what steps I should follow to take out the code from the "urls.py" and put the thing working in my "views.py" App?

Best Regards,

1
  • Just a note, "views.py" is not called a generic view. views.py is just a container for views (generic or normal). Usually generic views mean simple views that do one task and can be used in many situations. Commented Nov 9, 2011 at 20:34

2 Answers 2

1

Try to put (r'^search/', include('haystack.urls')),. You also might want to read "Getting started with Haystack"

Try:

#your root urls.py
from django.conf.urls.defaults import *
from haystack.forms import FacetedSearchForm
from haystack.query import SearchQuerySet
from haystack.views import FacetedSearchView

sqs = SearchQuerySet().filter(author='john')

urlpatterns = patterns('haystack.views',
url(r'^/my_custom_very_special_url$', FacetedSearchView(
    template='my/special/path/to/faceted_search.html',
    searchqueryset=sqs,
    form_class=FacetedSearchForm
), name='haystack_search'),
)

BTW, it's all in the docs.

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

5 Comments

Thanks for your reply. The problem is that I need to use other template that the one that is defined in Haystack "views.py". There is some way to pass the variables to the template from my App "views.py"?
@Andre, you need only template and nothing else?
Thanks for the reply Ivan, I need to Faceted search to my template and call other name than "search/" in the URI.
Thank for the reply. The code works. The only problem is when I use " 'haystack.views', " in the urlpatterns, combined with other URIs from my App, It brokes, I can't access any of URIs. If I use only " 'haystack.views', url(r'^resultados/$', FacetedSearchView(template='myapp_tmp/pgresults.html', searchqueryset=sqs, form_class=FacetedSearchForm), name='haystack_search'), " it works well. Any clue about what is happen here, I see that the problem is the " 'haystack.views', " in the urlpatterns. Thanks a lot for your help.
It is working now. I must to put that two patterns in the beginning. Thanks a lot.
1

Did you follow all the steps in the docs?

Use the extra_context method to add or overwrite the other variables in the context.

Comments

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.