Django:我应该使用查询字符串还是干净的 url 来映射显示参数?又如何呢?

发布于 2024-11-15 00:40:49 字数 914 浏览 3 评论 0原文

我有以下 urlconf:

urlpatterns = patterns('page.manager.views',
    url(r'^$', 'pages', name='page_manager-pages'),
    url(r'^add/$', 'add_page', name='page_manager-add_page'),
    url(r'^(?P<page_id>\d+)/', include(object_patterns)),    
)

“页面”视图必须返回所有页面的对象列表。将通过侧面菜单向用户提供多个显示/搜索选项: - 创建:任意/过去 6 小时/12 小时/24 小时/周 - 状态:任意/status_1/status_2/status_3 - 持续时间:任意/duration_1/duration_2/duration_3 我

这些选项用于选择应向用户呈现哪些值并且可以组合使用,例如:created=any、status=status_1、duration=duration_1

的问题是,如何在 Django 中最好地实现这一点?

到目前为止我所拥有的: 我可以对列表对象的通用视图进行子类化,创建一个视图,该视图接受参数(创建、状态、持续时间等)并提供正确的查询集(使用与其他参数一起传递的所选[或默认]排序选项)。

为了传递这些参数,查询字符串似乎适合于此,因为我们是从资源(所有页面的列表)中进行选择。是/否?

我还知道我们从 request.GET.get('argument_name') 获取此信息。

但是如何创建搜索选项的链接呢?例如:任意、任意/status_1/status_2/status_3。我们需要知道哪些已经处于活动状态,所以...模板标签?也许有更简单的方法?

这是在 Django 中处理此类事情的正确解决方案,还是有更好的方法?

I have the following urlconf:

urlpatterns = patterns('page.manager.views',
    url(r'^

The 'pages' view must return an object list of all pages. The user will be offered several display/search options through a side menu:
- created: any/ past 6hours/12hours/24hours/week
- Status: any/ status_1/status_2/status_3
- Duration: any / duration_1/duration_2/duration_3
etc.

These options are used to select which values should be presented to the user and can be used in combination, eg: created=any, status=status_1, duration=duration_1

My question is, how best to achieve this in Django?

What I have so far:
I can subclass a generic view for list objects, creating a view which takes the arguments(created, status, duration, etc) and provides the proper queryset(with the chosen[or default] ordering options passed along with the other arguments).

To pass these arguments, query strings seem to be right for this, since we are selecting from a resource(the list of all pages). Yes/no?

I'm also aware we get this information from request.GET.get('argument_name').

But how to create the links for the search options? eg: any, any/ status_1/status_2/status_3. We need to know which are already active, so...template tag? An easier way perhaps?

Is this the proper solution to handle this type of thing in Django, or is there a better way?

, 'pages', name='page_manager-pages'), url(r'^add/

The 'pages' view must return an object list of all pages. The user will be offered several display/search options through a side menu:
- created: any/ past 6hours/12hours/24hours/week
- Status: any/ status_1/status_2/status_3
- Duration: any / duration_1/duration_2/duration_3
etc.

These options are used to select which values should be presented to the user and can be used in combination, eg: created=any, status=status_1, duration=duration_1

My question is, how best to achieve this in Django?

What I have so far:
I can subclass a generic view for list objects, creating a view which takes the arguments(created, status, duration, etc) and provides the proper queryset(with the chosen[or default] ordering options passed along with the other arguments).

To pass these arguments, query strings seem to be right for this, since we are selecting from a resource(the list of all pages). Yes/no?

I'm also aware we get this information from request.GET.get('argument_name').

But how to create the links for the search options? eg: any, any/ status_1/status_2/status_3. We need to know which are already active, so...template tag? An easier way perhaps?

Is this the proper solution to handle this type of thing in Django, or is there a better way?

, 'add_page', name='page_manager-add_page'), url(r'^(?P<page_id>\d+)/', include(object_patterns)), )

The 'pages' view must return an object list of all pages. The user will be offered several display/search options through a side menu:
- created: any/ past 6hours/12hours/24hours/week
- Status: any/ status_1/status_2/status_3
- Duration: any / duration_1/duration_2/duration_3
etc.

These options are used to select which values should be presented to the user and can be used in combination, eg: created=any, status=status_1, duration=duration_1

My question is, how best to achieve this in Django?

What I have so far:
I can subclass a generic view for list objects, creating a view which takes the arguments(created, status, duration, etc) and provides the proper queryset(with the chosen[or default] ordering options passed along with the other arguments).

To pass these arguments, query strings seem to be right for this, since we are selecting from a resource(the list of all pages). Yes/no?

I'm also aware we get this information from request.GET.get('argument_name').

But how to create the links for the search options? eg: any, any/ status_1/status_2/status_3. We need to know which are already active, so...template tag? An easier way perhaps?

Is this the proper solution to handle this type of thing in Django, or is there a better way?

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

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

发布评论

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

评论(1

少女七分熟 2024-11-22 00:40:49

由于您有离散的、可选的和无序的知识片段有助于您的查询,因此我认为 GET 是最好的方法。另外,请注意 request.GET 是一个字典(因此,您可以执行 request.GET['status'])。

至于搜索选项,我想说模板标签和上下文变量可能合适,具体取决于您的视图的详细信息。我最可能的方法是用需要显示的 True / False 标志填充上下文字典,然后使用 {% if %} 块在模板中对其进行排序。

Since you have discrete, optional and unordered pieces of knowledge contributing to your query, I think that GET is the best way. Also, note that request.GET is a dict (ergo, you can do request.GET['status']).

As for the search options, I'd say a template tag and a context variable might either be appropriate depending on the details of your view. My most likely approach is to populate a context dict with True / False flags for which need to be displayed and then have {% if %} blocks to sort it out in the template.

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