Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.9k views
in Technique[技术] by (71.8m points)

python - Creating a generic Search View that returns different template according to origin View

I have a search bar in every template of my project, and I'd like for it to run different lookups for different models according to the template (or view) that you click search on.

For example: If you are currently in a page that lists Model A objects, the search bar should take the input and do a lookup for Model A.

I've managed to do it like so:

def Busqueda(request):
""" Esta vista se encarga de buscar los parámetros introducidos en
la barra de búsqueda. """

path = request.META['HTTP_REFERER']

b = request.GET.get('q')

if 'articulo' in path or 'tablon' in path:
    # Búsqueda de artículos
    queryset = Articulo.objects.exclude(
        estado='r'
    ).filter(
        Q(producto__nombre_amistoso__icontains=b) |
        Q(producto__nombre_amistoso__icontains=b) |
        Q(producto__referencia__icontains=b)
    ).distinct(
    ).order_by('-nota__fecha')

    paginacion = Paginator(queryset, 15)
    pagina = request.GET.get('page')
    resultado = paginacion.get_page(pagina)
    # TODO: Crear plantilla para resultados_articulos
    return render(
        request,
        "gestion/r_articulos.html",
        {'resultado': resultado}
    )

if 'producto' in path:
    # Búsqueda de productos
    queryset = Producto.objects.filter(
        Q(nombre_amistoso__icontains=b) |
        Q(nombre_amistoso__icontains=b) |
        Q(referencia__icontains=b)
    ).order_by('nombre_amistoso')

    paginacion = Paginator(queryset, 15)
    pagina = request.GET.get('page')
    resultado = paginacion.get_page(pagina)
    # TODO: Crear plantilla para resultados_productos
    return render(
        request,
        "gestion/r_productos.html",
        {'resultado': resultado}
    )

if 'pedido' in path:
    # Búsqueda de Pedidos
    queryset = Pedido.objects.filter(
        Q(codigo__icontains=b) |
        Q(cpm__icontains=b) |
        Q(distribuidor__nomre__icontains=b)
    ).order_by('-fecha_creacion')

    paginacion = Paginator(queryset, 15)
    pagina = request.GET.get('page')
    resultado = paginacion.get_page(pagina)
    # Crear plantilla para resultados_pedidos
    return render(
        request,
        "gestion/r_pedidos.html",
        {'resultado': resultado}
    )

I'm not a programmer, but this already smells bad to me. First problem I could be facing is a browser not supporting META_REFERRER.

How would you guys go about this? I'm sure there's a smarter way.

Thanks a lot for your time!

J.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...