Django:URLconf 中的变量参数
我一直在寻找这个问题,但找不到任何问题,如果重复的话,抱歉。
我正在建立某种电子商务网站,类似于 eBay。当我尝试浏览“类别”和“过滤器”时出现了问题。例如。您可以浏览“监视器”类别。这将向您显示大量监视器和一些应用它们的过滤器(与 eBay 完全相同)。因此,您转到“显示器”,然后您会看到如下过滤器:
- 类型:LCD - LED - CRT
- 品牌:ViewSonic - LG - Samsung
- 最大分辨率:800x600 - 1024x768
这些过滤器将附加到 URL,以下是示例:当您浏览显示器时,URL 可能类似于:
store.com/monitors
如果您应用“类型”过滤器:
store.com/monitors/LCD
“品牌”:
store.com/monitors/LCD/LG
“最大分辨率”:
store.com/monitors/LCD/LG/1024x768
因此,总结一下,URL 结构将类似于:
/category/filter1/filter2/filter3
I真的不知道该怎么做。问题是过滤器可能是可变的。我认为在视图中需要使用 **kwargs
但我不太确定。
您知道如何捕获此类参数吗?
多谢!
I've been looking for this question and couldn't find any, sorry if it's duplicated.
I'm building some kind of ecommerce site, similar to ebay. The problem i have arise when i'm trying to browse through "categories" and "filters". For example. You can browse the "Monitor" category. That will show you lots of monitors, and some filters (exactly the same as ebay) to apply them. So, you go to "monitors", then you have filters like:
- Type: LCD - LED - CRT
- Brand: ViewSonic - LG - Samsung
- Max Resolution: 800x600 - 1024x768
And those filters will be appended to the URL, following with the example, when you browse monitors the URL could be something like:
store.com/monitors
If you apply the "Type" filter:
store.com/monitors/LCD
"Brand":
store.com/monitors/LCD/LG
"Max Resolution":
store.com/monitors/LCD/LG/1024x768
So, summarizing, the URL structure would be something like:
/category/filter1/filter2/filter3
I can't figure out how to do it really. The problem is that filters can be variable. I think in the view will need to use **kwargs
but i'm not really sure.
Do you have any idea how to capture that kind of parameters?
Thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Ben,我希望这对您有帮助
urls.py
view.py
或
Ben, I hope this will help you
urls.py
view.py
or
您可以将其添加到您的网址中:
然后 myview 将获取类别和过滤器的参数。您可以按“/”拆分过滤器并在过滤器表中搜索每个部分。
这有道理吗?
You could add this to your urls:
And then myview would get the parameters of category and filters. You could split filters on "/" and search for each part within the Filters table.
Does that make sense?
您打算如何决定要过滤哪个方面?您是否有每个类别可接受的关键字列表?即服务器如何知道这
意味着
type=LCD,brand=LG
,但
不意味着
type=LG,brand=LCD
等您有任何理由不想使用 GET 参数,例如
how do you intend to decide what aspect is being filtered by? Do you have a list of accepted keywords for each category? ie how does the server know that
means
type=LCD, brand=LG
but
doesn't mean
type=LG, brand=LCD
etcIs there any reason you don't want to use GET params, e.g.