<i id='29cmB'><tr id='29cmB'><dt id='29cmB'><q id='29cmB'><span id='29cmB'><b id='29cmB'><form id='29cmB'><ins id='29cmB'></ins><ul id='29cmB'></ul><sub id='29cmB'></sub></form><legend id='29cmB'></legend><bdo id='29cmB'><pre id='29cmB'><center id='29cmB'></center></pre></bdo></b><th id='29cmB'></th></span></q></dt></tr></i><div id='29cmB'><tfoot id='29cmB'></tfoot><dl id='29cmB'><fieldset id='29cmB'></fieldset></dl></div>

        <tfoot id='29cmB'></tfoot>

          <bdo id='29cmB'></bdo><ul id='29cmB'></ul>

        <small id='29cmB'></small><noframes id='29cmB'>

        <legend id='29cmB'><style id='29cmB'><dir id='29cmB'><q id='29cmB'></q></dir></style></legend>
      1. Django 可选 url 参数

        Django optional url parameters(Django 可选 url 参数)
        • <legend id='OKtWe'><style id='OKtWe'><dir id='OKtWe'><q id='OKtWe'></q></dir></style></legend>

          <i id='OKtWe'><tr id='OKtWe'><dt id='OKtWe'><q id='OKtWe'><span id='OKtWe'><b id='OKtWe'><form id='OKtWe'><ins id='OKtWe'></ins><ul id='OKtWe'></ul><sub id='OKtWe'></sub></form><legend id='OKtWe'></legend><bdo id='OKtWe'><pre id='OKtWe'><center id='OKtWe'></center></pre></bdo></b><th id='OKtWe'></th></span></q></dt></tr></i><div id='OKtWe'><tfoot id='OKtWe'></tfoot><dl id='OKtWe'><fieldset id='OKtWe'></fieldset></dl></div>
        • <small id='OKtWe'></small><noframes id='OKtWe'>

          <tfoot id='OKtWe'></tfoot>
            <bdo id='OKtWe'></bdo><ul id='OKtWe'></ul>
              <tbody id='OKtWe'></tbody>

                • 本文介绍了Django 可选 url 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I have a Django URL like this:

                  url(
                      r'^project_config/(?P<product>w+)/(?P<project_id>w+)/$',
                      'tool.views.ProjectConfig',
                      name='project_config'
                  ),
                  

                  views.py:

                  def ProjectConfig(request, product, project_id=None, template_name='project.html'):
                      ...
                      # do stuff
                  

                  The problem is that I want the project_id parameter to be optional.

                  I want /project_config/ and /project_config/12345abdce/ to be equally valid URL patterns, so that if project_id is passed, then I can use it.

                  As it stands at the moment, I get a 404 when I access the URL without the project_id parameter.

                  解决方案

                  There are several approaches.

                  One is to use a non-capturing group in the regex: (?:/(?P<title>[a-zA-Z]+)/)?
                  Making a Regex Django URL Token Optional

                  Another, easier to follow way is to have multiple rules that matches your needs, all pointing to the same view.

                  urlpatterns = patterns('',
                      url(r'^project_config/$', views.foo),
                      url(r'^project_config/(?P<product>w+)/$', views.foo),
                      url(r'^project_config/(?P<product>w+)/(?P<project_id>w+)/$', views.foo),
                  )
                  

                  Keep in mind that in your view you'll also need to set a default for the optional URL parameter, or you'll get an error:

                  def foo(request, optional_parameter=''):
                      # Your code goes here
                  

                  这篇关于Django 可选 url 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)
                  Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)
                  Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)
                  Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)
                  Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)
                  Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)
                    <tbody id='UiC5n'></tbody>
                  • <legend id='UiC5n'><style id='UiC5n'><dir id='UiC5n'><q id='UiC5n'></q></dir></style></legend>

                      <small id='UiC5n'></small><noframes id='UiC5n'>

                        <bdo id='UiC5n'></bdo><ul id='UiC5n'></ul>
                        <i id='UiC5n'><tr id='UiC5n'><dt id='UiC5n'><q id='UiC5n'><span id='UiC5n'><b id='UiC5n'><form id='UiC5n'><ins id='UiC5n'></ins><ul id='UiC5n'></ul><sub id='UiC5n'></sub></form><legend id='UiC5n'></legend><bdo id='UiC5n'><pre id='UiC5n'><center id='UiC5n'></center></pre></bdo></b><th id='UiC5n'></th></span></q></dt></tr></i><div id='UiC5n'><tfoot id='UiC5n'></tfoot><dl id='UiC5n'><fieldset id='UiC5n'></fieldset></dl></div>
                        <tfoot id='UiC5n'></tfoot>