Django:URLconf 中的变量参数

发布于 2024-12-20 03:24:10 字数 786 浏览 1 评论 0原文

我一直在寻找这个问题,但找不到任何问题,如果重复的话,抱歉。

我正在建立某种电子商务网站,类似于 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 技术交流群。

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

发布评论

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

评论(3

梓梦 2024-12-27 03:24:10

Ben,我希望这对您有帮助

urls.py

from catalog.views import catalog_products_view

urlpatterns = patterns(
    '',
    url(r'^(?P<category>[\w-]+)/

view.py

def catalog_products_view(request, category, filter1=None, filter2=None, filter3=None):
    # some code here

def catalog_products_view(request, category, **kwargs):
    filter1 = kwargs['filter1']
    filter2 = kwargs['filter2']
    ....
    filterN = kwargs['filterN']
    # some code here
, catalog_products_view, name="catalog_products_view"), url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/

view.py



, catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/

view.py



, catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/(?P<filter3>[\w-]+)/

view.py



, catalog_products_view, name="catalog_products_view"),
)

view.py

Ben, I hope this will help you

urls.py

from catalog.views import catalog_products_view

urlpatterns = patterns(
    '',
    url(r'^(?P<category>[\w-]+)/

view.py

def catalog_products_view(request, category, filter1=None, filter2=None, filter3=None):
    # some code here

or

def catalog_products_view(request, category, **kwargs):
    filter1 = kwargs['filter1']
    filter2 = kwargs['filter2']
    ....
    filterN = kwargs['filterN']
    # some code here
, catalog_products_view, name="catalog_products_view"), url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/

view.py


or


, catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/

view.py


or


, catalog_products_view, name="catalog_products_view"),
    url(r'^(?P<category>[\w-]+)/(?P<filter1>[\w-]+)/(?P<filter2>[\w-]+)/(?P<filter3>[\w-]+)/

view.py


or


, catalog_products_view, name="catalog_products_view"),
)

view.py

or

沧笙踏歌 2024-12-27 03:24:10

您可以将其添加到您的网址中:

url(r'^(?P<category>\w)/(?P<filters>.*)/

然后 myview 将获取类别和过滤器的参数。您可以按“/”拆分过滤器并在过滤器表中搜索每个部分。

这有道理吗?

, 'myview'),

然后 myview 将获取类别和过滤器的参数。您可以按“/”拆分过滤器并在过滤器表中搜索每个部分。

这有道理吗?

You could add this to your urls:

url(r'^(?P<category>\w)/(?P<filters>.*)/

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?

, 'myview'),

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?

冷弦 2024-12-27 03:24:10

您打算如何决定要过滤哪个方面?您是否有每个类别可接受的关键字列表?即服务器如何知道这

/LCD/LG/

意味着type=LCD,brand=LG

,但

/LG/LCD

意味着type=LG,brand=LCD

等您有任何理由不想使用 GET 参数,例如

.../search/?make=LD&size=42

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

/LCD/LG/

means type=LCD, brand=LG

but

/LG/LCD

doesn't mean type=LG, brand=LCD etc

Is there any reason you don't want to use GET params, e.g.

.../search/?make=LD&size=42
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文