在 Django 中,通过搜索框和类别一次性搜索和过滤?

发布于 2024-09-12 18:55:04 字数 507 浏览 2 评论 0原文

我想知道你是否能帮助我。

我有一个将显示在一页上的数据列表。有一个简单的搜索框、一个类别列表和一个标签列表,都可以用来过滤数据列表。我试图从头开始构建它(因此它不需要 JavaScript),但最终它将提交搜索条件并使用 Ajax 返回一个新列表。

所以我的数据库中有一个类别列表(“大”、“小”等),并且我的数据库中有一个标签列表(“木制”、“黄铜”)。标签用于过滤类别中的更多内容。然后我有一个搜索框。理想情况下,我希望用户有效地勾选他们想要的类别,勾选他们想要的标签,并可能搜索关键字,然后提交所有这些数据,以便可以查询这些数据并返回已过滤数据的新列表。

我不是 Django 专家,我对如何以及在哪里执行此操作感到困惑... Django 将类别吐出为复选框列表、将标签吐出为复选框列表以及将搜索框吐出的方式是什么?提交按钮...提交后,我可以获取所有数据并对数据库进行必要的查询?我不太明白我该怎么做...我已经看了几天 Django Docs 和 Django Book,我做事的方式似乎没有列出。

拜托,任何帮助都会很棒。

I wonder if you could help me.

I have a list of data that will be displayed on one page. There is a simple search box, a list of categories and a list of tags that can all be used to filter the list of data. I'm trying to built it from the ground up (so it doesn't require JavaScript) but eventually it will submit the search criteria and return back a new list using Ajax.

So I have a list of categories in my database ('large', 'small', etc), and I have a list of tags in my database ('wooden', 'brass'). Tags are used to filter down more of what's in the categories. I then have a search box. Ideally, I want the user to effectively tick which categories they want, tick what tags they want and possibly put a search for keywords, and then submit all of that data so it can be queried and a new list of the filtered data can be returned.

I'm not a Django expert, and I'm stuck on how and where to do this... What is the Django way of spitting out the categories as a checkbox list, the tags as a checkbox list and the search box with a submit button... Which when submitted, I can take all that data and do the necessary queries on the database? I don't quite understand how I'd do this... I've been looking at the Django Docs and the Django Book for a few days and the way I'm doing things doesn't seem to be listed.

Please, any help at all would be fantastic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪#恶を代价 2024-09-19 18:55:04

将类别吐出为复选框列表,

作为复选框列表的标签和

带有提交按钮的搜索框...

这是 HTML 页面中的

。它可能与 Django 模型中的任何内容都不太匹配。这是一种或多或少手动构建的独特形式。

我可以获取所有数据并对数据库进行必要的查询吗?

这是一个视图函数。

你可能会有这样的事情。

objects= SomeModel.objects
if request.GET ... has categories ...
    objects = objects.filter( ... categories ... )
if request.GET ... has tags ...
    objects = objects.filter( ... tags ... )
if request.GET ... has search ...
    objects = objects.filter( something__contains( search ) )
return render_to_response( ... etc. ... )

我做事的方式似乎没有列出。

您超出了此处的教程范围。

该怎么办?

  1. 完成整个教程。一路走过去。每一步。它似乎没有解决您的问题,但您必须完成整个教程。

  2. 设计你的模型。你在问题中没有提到模型。这绝对是最重要、最根本的事情。

  3. 为该模型创建默认管理界面。让默认的管理界面正常工作并执行您想做的各种事情。它具有出色的搜索、类别和标签过滤功能。

    为了让默认管理员正常工作,您需要设计相当复杂的模型和表单功能。您可能需要向模型中添加方法函数以及选项和其他优点。

  4. 当您的管理页面非常接近您想要的内容后,您可以编写自己的自定义视图。


每个复选框都有不同的名称(“category_option_1”、“category_option_2”等)...我如何阅读这些?我不能只放置 request.POST['category_option_n']?

真的吗?为什么你的问题没有这么说

你是问这个吗?

for k in range(1024):
    name = 'category_option_{0}'.format(k)
    # Use request.POST.get(name,None) to build a `Q` object

spitting out the categories as a checkbox list,

the tags as a checkbox list and the

search box with a submit button...

This is a <form> in your HTML page. It probably doesn't match anything in the Django model very well. It's a unique form built more-or-less manually.

I can take all that data and do the necessary queries on the database?

That's a view function.

You'll probably have something like this.

objects= SomeModel.objects
if request.GET ... has categories ...
    objects = objects.filter( ... categories ... )
if request.GET ... has tags ...
    objects = objects.filter( ... tags ... )
if request.GET ... has search ...
    objects = objects.filter( something__contains( search ) )
return render_to_response( ... etc. ... )

the way I'm doing things doesn't seem to be listed.

You're beyond the tutorial here.

What to do?

  1. Do the ENTIRE tutorial. All the way through. Every step. It doesn't seem like it solves your problem, but you MUST do the ENTIRE tutorial.

  2. Design your model. You didn't mention the model in the question. It's the absolutely most important and fundamental thing.

  3. Create the default admin interface for that model. Get the default admin interface to work and do the kinds of things you'd like to do. It has great search, category and tag filtering.

    In order to get the default admin to work, you'll need to design fairly sophisticated model and form features. You'll probably have to add method functions to your model as well as choice items and other goodness.

  4. AFTER you have the admin page pretty close to what you want, you can write you own customized view.


each single checkbox has a different name ('category_option_1', 'category_option_2', etc.) ... How do I read these? I can't just put request.POST['category_option_n']?

Really? Why didn't your question say that?

Are you asking about this?

for k in range(1024):
    name = 'category_option_{0}'.format(k)
    # Use request.POST.get(name,None) to build a `Q` object
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文