/usr/share/doc/python-werkzeug-doc/html/routing.html is in python-werkzeug-doc 0.9.4+dfsg-1.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>URL Routing — Werkzeug 0.9.4 documentation</title>
<link rel="stylesheet" href="_static/werkzeug.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.9.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="Werkzeug 0.9.4 documentation" href="index.html" />
<link rel="next" title="WSGI Helpers" href="wsgi.html" />
<link rel="prev" title="Request / Response Objects" href="wrappers.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="wsgi.html" title="WSGI Helpers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="wrappers.html" title="Request / Response Objects"
accesskey="P">previous</a> |</li>
<li><a href="index.html">Werkzeug 0.9.4 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="module-werkzeug.routing">
<span id="url-routing"></span><span id="routing"></span><h1>URL Routing<a class="headerlink" href="#module-werkzeug.routing" title="Permalink to this headline">¶</a></h1>
<p>When it comes to combining multiple controller or view functions (however
you want to call them), you need a dispatcher. A simple way would be
applying regular expression tests on <tt class="docutils literal"><span class="pre">PATH_INFO</span></tt> and call registered
callback functions that return the value.</p>
<p>Werkzeug provides a much more powerful system, similar to <a class="reference external" href="http://routes.groovie.org/">Routes</a>. All the
objects mentioned on this page must be imported from <a class="reference internal" href="#module-werkzeug.routing" title="werkzeug.routing"><tt class="xref py py-mod docutils literal"><span class="pre">werkzeug.routing</span></tt></a>, not
from <a class="reference internal" href="unicode.html#module-werkzeug" title="werkzeug"><tt class="xref py py-mod docutils literal"><span class="pre">werkzeug</span></tt></a>!</p>
<div class="section" id="quickstart">
<h2>Quickstart<a class="headerlink" href="#quickstart" title="Permalink to this headline">¶</a></h2>
<p>Here is a simple example which could be the URL definition for a blog:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="kn">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span><span class="p">,</span> <span class="n">NotFound</span><span class="p">,</span> <span class="n">RequestRedirect</span>
<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/index'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/<int:year>/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/archive'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/<int:year>/<int:month>/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/archive'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/<int:year>/<int:month>/<int:day>/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/archive'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/<int:year>/<int:month>/<int:day>/<slug>'</span><span class="p">,</span>
<span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/show_post'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/about'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/about_me'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/feeds/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/feeds'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/feeds/<feed_name>.rss'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/show_feed'</span><span class="p">)</span>
<span class="p">])</span>
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="n">urls</span> <span class="o">=</span> <span class="n">url_map</span><span class="o">.</span><span class="n">bind_to_environ</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">endpoint</span><span class="p">,</span> <span class="n">args</span> <span class="o">=</span> <span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">()</span>
<span class="k">except</span> <span class="n">HTTPException</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
<span class="k">return</span> <span class="n">e</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">)</span>
<span class="n">start_response</span><span class="p">(</span><span class="s">'200 OK'</span><span class="p">,</span> <span class="p">[(</span><span class="s">'Content-Type'</span><span class="p">,</span> <span class="s">'text/plain'</span><span class="p">)])</span>
<span class="k">return</span> <span class="p">[</span><span class="s">'Rule points to </span><span class="si">%r</span><span class="s"> with arguments </span><span class="si">%r</span><span class="s">'</span> <span class="o">%</span> <span class="p">(</span><span class="n">endpoint</span><span class="p">,</span> <span class="n">args</span><span class="p">)]</span>
</pre></div>
</div>
<p>So what does that do? First of all we create a new <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a> which stores
a bunch of URL rules. Then we pass it a list of <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><tt class="xref py py-class docutils literal"><span class="pre">Rule</span></tt></a> objects.</p>
<p>Each <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><tt class="xref py py-class docutils literal"><span class="pre">Rule</span></tt></a> object is instantiated with a string that represents a rule
and an endpoint which will be the alias for what view the rule represents.
Multiple rules can have the same endpoint, but should have different arguments
to allow URL construction.</p>
<p>The format for the URL rules is straightforward, but explained in detail below.</p>
<p>Inside the WSGI application we bind the url_map to the current request which will
return a new <a class="reference internal" href="#werkzeug.routing.MapAdapter" title="werkzeug.routing.MapAdapter"><tt class="xref py py-class docutils literal"><span class="pre">MapAdapter</span></tt></a>. This url_map adapter can then be used to match
or build domains for the current request.</p>
<p>The <a class="reference internal" href="#werkzeug.routing.MapAdapter.match" title="werkzeug.routing.MapAdapter.match"><tt class="xref py py-meth docutils literal"><span class="pre">MapAdapter.match()</span></tt></a> method can then either return a tuple in the form
<tt class="docutils literal"><span class="pre">(endpoint,</span> <span class="pre">args)</span></tt> or raise one of the three exceptions
<a class="reference internal" href="exceptions.html#werkzeug.exceptions.NotFound" title="werkzeug.exceptions.NotFound"><tt class="xref py py-exc docutils literal"><span class="pre">NotFound</span></tt></a>, <a class="reference internal" href="exceptions.html#werkzeug.exceptions.MethodNotAllowed" title="werkzeug.exceptions.MethodNotAllowed"><tt class="xref py py-exc docutils literal"><span class="pre">MethodNotAllowed</span></tt></a>,
or <tt class="xref py py-exc docutils literal"><span class="pre">RequestRedirect</span></tt>. For more details about those
exceptions have a look at the documentation of the <a class="reference internal" href="#werkzeug.routing.MapAdapter.match" title="werkzeug.routing.MapAdapter.match"><tt class="xref py py-meth docutils literal"><span class="pre">MapAdapter.match()</span></tt></a> method.</p>
</div>
<div class="section" id="rule-format">
<h2>Rule Format<a class="headerlink" href="#rule-format" title="Permalink to this headline">¶</a></h2>
<p>Rule strings basically are just normal URL paths with placeholders in the
format <tt class="docutils literal"><span class="pre"><converter(arguments):name></span></tt>, where converter and the arguments
are optional. If no converter is defined, the <cite>default</cite> converter is used
(which means <cite>string</cite> in the normal configuration).</p>
<p>URL rules that end with a slash are branch URLs, others are leaves. If you
have <cite>strict_slashes</cite> enabled (which is the default), all branch URLs that are
visited without a trailing slash will trigger a redirect to the same URL with
that slash appended.</p>
<p>The list of converters can be extended, the default converters are explained
below.</p>
</div>
<div class="section" id="builtin-converters">
<h2>Builtin Converters<a class="headerlink" href="#builtin-converters" title="Permalink to this headline">¶</a></h2>
<p>Here a list of converters that come with Werkzeug:</p>
<dl class="class">
<dt id="werkzeug.routing.UnicodeConverter">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">UnicodeConverter</tt><big>(</big><em>map</em>, <em>minlength=1</em>, <em>maxlength=None</em>, <em>length=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.UnicodeConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter is the default converter and accepts any string but
only one path segment. Thus the string can not include a slash.</p>
<p>This is the default validator.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Rule</span><span class="p">(</span><span class="s">'/pages/<page>'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/<string(length=2):lang_code>'</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a>.</li>
<li><strong>minlength</strong> – the minimum length of the string. Must be greater
or equal 1.</li>
<li><strong>maxlength</strong> – the maximum length of the string.</li>
<li><strong>length</strong> – the exact length of the string.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.PathConverter">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">PathConverter</tt><big>(</big><em>map</em><big>)</big><a class="headerlink" href="#werkzeug.routing.PathConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>Like the default <a class="reference internal" href="#werkzeug.routing.UnicodeConverter" title="werkzeug.routing.UnicodeConverter"><tt class="xref py py-class docutils literal"><span class="pre">UnicodeConverter</span></tt></a>, but it also matches
slashes. This is useful for wikis and similar applications:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Rule</span><span class="p">(</span><span class="s">'/<path:wikipage>'</span><span class="p">)</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/<path:wikipage>/edit'</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a>.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.AnyConverter">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">AnyConverter</tt><big>(</big><em>map</em>, <em>*items</em><big>)</big><a class="headerlink" href="#werkzeug.routing.AnyConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>Matches one of the items provided. Items can either be Python
identifiers or strings:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Rule</span><span class="p">(</span><span class="s">'/<any(about, help, imprint, class, "foo,bar"):page_name>'</span><span class="p">)</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a>.</li>
<li><strong>items</strong> – this function accepts the possible items as positional
arguments.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.IntegerConverter">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">IntegerConverter</tt><big>(</big><em>map</em>, <em>fixed_digits=0</em>, <em>min=None</em>, <em>max=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.IntegerConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter only accepts integer values:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Rule</span><span class="p">(</span><span class="s">'/page/<int:page>'</span><span class="p">)</span>
</pre></div>
</div>
<p>This converter does not support negative values.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a>.</li>
<li><strong>fixed_digits</strong> – the number of fixed digits in the URL. If you set
this to <tt class="docutils literal"><span class="pre">4</span></tt> for example, the application will
only match if the url looks like <tt class="docutils literal"><span class="pre">/0001/</span></tt>. The
default is variable length.</li>
<li><strong>min</strong> – the minimal value.</li>
<li><strong>max</strong> – the maximal value.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.FloatConverter">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">FloatConverter</tt><big>(</big><em>map</em>, <em>min=None</em>, <em>max=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.FloatConverter" title="Permalink to this definition">¶</a></dt>
<dd><p>This converter only accepts floating point values:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Rule</span><span class="p">(</span><span class="s">'/probability/<float:probability>'</span><span class="p">)</span>
</pre></div>
</div>
<p>This converter does not support negative values.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>map</strong> – the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a>.</li>
<li><strong>min</strong> – the minimal value.</li>
<li><strong>max</strong> – the maximal value.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</div>
<div class="section" id="maps-rules-and-adapters">
<h2>Maps, Rules and Adapters<a class="headerlink" href="#maps-rules-and-adapters" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.routing.Map">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">Map</tt><big>(</big><em>rules=None</em>, <em>default_subdomain=''</em>, <em>charset='utf-8'</em>, <em>strict_slashes=True</em>, <em>redirect_defaults=True</em>, <em>converters=None</em>, <em>sort_parameters=False</em>, <em>sort_key=None</em>, <em>encoding_errors='replace'</em>, <em>host_matching=False</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Map" title="Permalink to this definition">¶</a></dt>
<dd><p>The map class stores all the URL rules and some configuration
parameters. Some of the configuration values are only stored on the
<cite>Map</cite> instance since those affect all rules, others are just defaults
and can be overridden for each rule. Note that you have to specify all
arguments besides the <cite>rules</cite> as keyword arguments!</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>rules</strong> – sequence of url rules for this map.</li>
<li><strong>default_subdomain</strong> – The default subdomain for rules without a
subdomain defined.</li>
<li><strong>charset</strong> – charset of the url. defaults to <tt class="docutils literal"><span class="pre">"utf-8"</span></tt></li>
<li><strong>strict_slashes</strong> – Take care of trailing slashes.</li>
<li><strong>redirect_defaults</strong> – This will redirect to the default rule if it
wasn’t visited that way. This helps creating
unique URLs.</li>
<li><strong>converters</strong> – A dict of converters that adds additional converters
to the list of converters. If you redefine one
converter this will override the original one.</li>
<li><strong>sort_parameters</strong> – If set to <cite>True</cite> the url parameters are sorted.
See <cite>url_encode</cite> for more details.</li>
<li><strong>sort_key</strong> – The sort key function for <cite>url_encode</cite>.</li>
<li><strong>encoding_errors</strong> – the error method to use for decoding</li>
<li><strong>host_matching</strong> – if set to <cite>True</cite> it enables the host matching
feature and disables the subdomain one. If
enabled the <cite>host</cite> parameter to rules is used
instead of the <cite>subdomain</cite> one.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.5: </span><cite>sort_parameters</cite> and <cite>sort_key</cite> was added.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7: </span><cite>encoding_errors</cite> and <cite>host_matching</cite> was added.</p>
</div>
<dl class="attribute">
<dt id="werkzeug.routing.Map.converters">
<tt class="descname">converters</tt><a class="headerlink" href="#werkzeug.routing.Map.converters" title="Permalink to this definition">¶</a></dt>
<dd><p>The dictionary of converters. This can be modified after the class
was created, but will only affect rules added after the
modification. If the rules are defined with the list passed to the
class, the <cite>converters</cite> parameter to the constructor has to be used
instead.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.Map.add">
<tt class="descname">add</tt><big>(</big><em>rulefactory</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Map.add" title="Permalink to this definition">¶</a></dt>
<dd><p>Add a new rule or factory to the map and bind it. Requires that the
rule is not bound to another map.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>rulefactory</strong> – a <a class="reference internal" href="#werkzeug.routing.Rule" title="werkzeug.routing.Rule"><tt class="xref py py-class docutils literal"><span class="pre">Rule</span></tt></a> or <a class="reference internal" href="#werkzeug.routing.RuleFactory" title="werkzeug.routing.RuleFactory"><tt class="xref py py-class docutils literal"><span class="pre">RuleFactory</span></tt></a></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.Map.bind">
<tt class="descname">bind</tt><big>(</big><em>server_name</em>, <em>script_name=None</em>, <em>subdomain=None</em>, <em>url_scheme='http'</em>, <em>default_method='GET'</em>, <em>path_info=None</em>, <em>query_args=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Map.bind" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new <a class="reference internal" href="#werkzeug.routing.MapAdapter" title="werkzeug.routing.MapAdapter"><tt class="xref py py-class docutils literal"><span class="pre">MapAdapter</span></tt></a> with the details specified to the
call. Note that <cite>script_name</cite> will default to <tt class="docutils literal"><span class="pre">'/'</span></tt> if not further
specified or <cite>None</cite>. The <cite>server_name</cite> at least is a requirement
because the HTTP RFC requires absolute URLs for redirects and so all
redirect exceptions raised by Werkzeug will contain the full canonical
URL.</p>
<p>If no path_info is passed to <tt class="xref py py-meth docutils literal"><span class="pre">match()</span></tt> it will use the default path
info passed to bind. While this doesn’t really make sense for
manual bind calls, it’s useful if you bind a map to a WSGI
environment which already contains the path info.</p>
<p><cite>subdomain</cite> will default to the <cite>default_subdomain</cite> for this map if
no defined. If there is no <cite>default_subdomain</cite> you cannot use the
subdomain feature.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7: </span><cite>query_args</cite> added</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.8: </span><cite>query_args</cite> can now also be a string.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.Map.bind_to_environ">
<tt class="descname">bind_to_environ</tt><big>(</big><em>environ</em>, <em>server_name=None</em>, <em>subdomain=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Map.bind_to_environ" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <a class="reference internal" href="#werkzeug.routing.Map.bind" title="werkzeug.routing.Map.bind"><tt class="xref py py-meth docutils literal"><span class="pre">bind()</span></tt></a> but you can pass it an WSGI environment and it
will fetch the information from that dictionary. Note that because of
limitations in the protocol there is no way to get the current
subdomain and real <cite>server_name</cite> from the environment. If you don’t
provide it, Werkzeug will use <cite>SERVER_NAME</cite> and <cite>SERVER_PORT</cite> (or
<cite>HTTP_HOST</cite> if provided) as used <cite>server_name</cite> with disabled subdomain
feature.</p>
<p>If <cite>subdomain</cite> is <cite>None</cite> but an environment and a server name is
provided it will calculate the current subdomain automatically.
Example: <cite>server_name</cite> is <tt class="docutils literal"><span class="pre">'example.com'</span></tt> and the <cite>SERVER_NAME</cite>
in the wsgi <cite>environ</cite> is <tt class="docutils literal"><span class="pre">'staging.dev.example.com'</span></tt> the calculated
subdomain will be <tt class="docutils literal"><span class="pre">'staging.dev'</span></tt>.</p>
<p>If the object passed as environ has an environ attribute, the value of
this attribute is used instead. This allows you to pass request
objects. Additionally <cite>PATH_INFO</cite> added as a default of the
<a class="reference internal" href="#werkzeug.routing.MapAdapter" title="werkzeug.routing.MapAdapter"><tt class="xref py py-class docutils literal"><span class="pre">MapAdapter</span></tt></a> so that you don’t have to pass the path info to
the match method.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.5: </span>previously this method accepted a bogus <cite>calculate_subdomain</cite>
parameter that did not have any effect. It was removed because
of that.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.8: </span>This will no longer raise a ValueError when an unexpected server
name was passed.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>environ</strong> – a WSGI environment.</li>
<li><strong>server_name</strong> – an optional server name hint (see above).</li>
<li><strong>subdomain</strong> – optionally the current subdomain (see above).</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="attribute">
<dt id="werkzeug.routing.Map.default_converters">
<tt class="descname">default_converters</tt><em class="property"> = ImmutableDict({'int': <class 'werkzeug.routing.IntegerConverter'>, 'string': <class 'werkzeug.routing.UnicodeConverter'>, 'default': <class 'werkzeug.routing.UnicodeConverter'>, 'path': <class 'werkzeug.routing.PathConverter'>, 'float': <class 'werkzeug.routing.FloatConverter'>, 'any': <class 'werkzeug.routing.AnyConverter'>})</em><a class="headerlink" href="#werkzeug.routing.Map.default_converters" title="Permalink to this definition">¶</a></dt>
<dd><div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>a dict of default converters to be used.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.Map.is_endpoint_expecting">
<tt class="descname">is_endpoint_expecting</tt><big>(</big><em>endpoint</em>, <em>*arguments</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Map.is_endpoint_expecting" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over all rules and check if the endpoint expects
the arguments provided. This is for example useful if you have
some URLs that expect a language code and others that do not and
you want to wrap the builder a bit so that the current language
code is automatically added if not provided but endpoints expect
it.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>endpoint</strong> – the endpoint to check.</li>
<li><strong>arguments</strong> – this function accepts one or more arguments
as positional arguments. Each one of them is
checked.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.Map.iter_rules">
<tt class="descname">iter_rules</tt><big>(</big><em>endpoint=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Map.iter_rules" title="Permalink to this definition">¶</a></dt>
<dd><p>Iterate over all rules or the rules of an endpoint.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>endpoint</strong> – if provided only the rules for that endpoint
are returned.</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body">an iterator</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.Map.update">
<tt class="descname">update</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.routing.Map.update" title="Permalink to this definition">¶</a></dt>
<dd><p>Called before matching and building to keep the compiled rules
in the correct order after things changed.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.MapAdapter">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">MapAdapter</tt><big>(</big><em>map</em>, <em>server_name</em>, <em>script_name</em>, <em>subdomain</em>, <em>url_scheme</em>, <em>path_info</em>, <em>default_method</em>, <em>query_args=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter" title="Permalink to this definition">¶</a></dt>
<dd><p>Returned by <a class="reference internal" href="#werkzeug.routing.Map.bind" title="werkzeug.routing.Map.bind"><tt class="xref py py-meth docutils literal"><span class="pre">Map.bind()</span></tt></a> or <a class="reference internal" href="#werkzeug.routing.Map.bind_to_environ" title="werkzeug.routing.Map.bind_to_environ"><tt class="xref py py-meth docutils literal"><span class="pre">Map.bind_to_environ()</span></tt></a> and does
the URL matching and building based on runtime information.</p>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.allowed_methods">
<tt class="descname">allowed_methods</tt><big>(</big><em>path_info=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.allowed_methods" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the valid methods that match for a given path.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.build">
<tt class="descname">build</tt><big>(</big><em>endpoint</em>, <em>values=None</em>, <em>method=None</em>, <em>force_external=False</em>, <em>append_unknown=True</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.build" title="Permalink to this definition">¶</a></dt>
<dd><p>Building URLs works pretty much the other way round. Instead of
<cite>match</cite> you call <cite>build</cite> and pass it the endpoint and a dict of
arguments for the placeholders.</p>
<p>The <cite>build</cite> function also accepts an argument called <cite>force_external</cite>
which, if you set it to <cite>True</cite> will force external URLs. Per default
external URLs (include the server name) will only be used if the
target URL is on a different subdomain.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="gp">... </span> <span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Rule</span><span class="p">(</span><span class="s">'/downloads/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'downloads/index'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Rule</span><span class="p">(</span><span class="s">'/downloads/<int:id>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'downloads/show'</span><span class="p">)</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">urls</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s">"example.com"</span><span class="p">,</span> <span class="s">"/"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s">"index"</span><span class="p">,</span> <span class="p">{})</span>
<span class="go">'/'</span>
<span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s">"downloads/show"</span><span class="p">,</span> <span class="p">{</span><span class="s">'id'</span><span class="p">:</span> <span class="mi">42</span><span class="p">})</span>
<span class="go">'/downloads/42'</span>
<span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s">"downloads/show"</span><span class="p">,</span> <span class="p">{</span><span class="s">'id'</span><span class="p">:</span> <span class="mi">42</span><span class="p">},</span> <span class="n">force_external</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="go">'http://example.com/downloads/42'</span>
</pre></div>
</div>
<p>Because URLs cannot contain non ASCII data you will always get
bytestrings back. Non ASCII characters are urlencoded with the
charset defined on the map instance.</p>
<p>Additional values are converted to unicode and appended to the URL as
URL querystring parameters:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">build</span><span class="p">(</span><span class="s">"index"</span><span class="p">,</span> <span class="p">{</span><span class="s">'q'</span><span class="p">:</span> <span class="s">'My Searchstring'</span><span class="p">})</span>
<span class="go">'/?q=My+Searchstring'</span>
</pre></div>
</div>
<p>If a rule does not exist when building a <cite>BuildError</cite> exception is
raised.</p>
<p>The build method accepts an argument called <cite>method</cite> which allows you
to specify the method you want to have an URL built for if you have
different methods for the same endpoint specified.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span>the <cite>append_unknown</cite> parameter was added.</p>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>endpoint</strong> – the endpoint of the URL to build.</li>
<li><strong>values</strong> – the values for the URL to build. Unhandled values are
appended to the URL as query parameters.</li>
<li><strong>method</strong> – the HTTP method for the rule if there are different
URLs for different methods on the same endpoint.</li>
<li><strong>force_external</strong> – enforce full canonical external URLs.</li>
<li><strong>append_unknown</strong> – unknown parameters are appended to the generated
URL as query string argument. Disable this
if you want the builder to ignore those.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.dispatch">
<tt class="descname">dispatch</tt><big>(</big><em>view_func</em>, <em>path_info=None</em>, <em>method=None</em>, <em>catch_http_exceptions=False</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.dispatch" title="Permalink to this definition">¶</a></dt>
<dd><p>Does the complete dispatching process. <cite>view_func</cite> is called with
the endpoint and a dict with the values for the view. It should
look up the view function, call it, and return a response object
or WSGI application. http exceptions are not caught by default
so that applications can display nicer error messages by just
catching them by hand. If you want to stick with the default
error messages you can pass it <tt class="docutils literal"><span class="pre">catch_http_exceptions=True</span></tt> and
it will catch the http exceptions.</p>
<p>Here a small example for the dispatch usage:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.wrappers</span> <span class="kn">import</span> <span class="n">Request</span><span class="p">,</span> <span class="n">Response</span>
<span class="kn">from</span> <span class="nn">werkzeug.wsgi</span> <span class="kn">import</span> <span class="n">responder</span>
<span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="kn">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span>
<span class="k">def</span> <span class="nf">on_index</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="k">return</span> <span class="n">Response</span><span class="p">(</span><span class="s">'Hello from the index'</span><span class="p">)</span>
<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span><span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">)])</span>
<span class="n">views</span> <span class="o">=</span> <span class="p">{</span><span class="s">'index'</span><span class="p">:</span> <span class="n">on_index</span><span class="p">}</span>
<span class="nd">@responder</span>
<span class="k">def</span> <span class="nf">application</span><span class="p">(</span><span class="n">environ</span><span class="p">,</span> <span class="n">start_response</span><span class="p">):</span>
<span class="n">request</span> <span class="o">=</span> <span class="n">Request</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
<span class="n">urls</span> <span class="o">=</span> <span class="n">url_map</span><span class="o">.</span><span class="n">bind_to_environ</span><span class="p">(</span><span class="n">environ</span><span class="p">)</span>
<span class="k">return</span> <span class="n">urls</span><span class="o">.</span><span class="n">dispatch</span><span class="p">(</span><span class="k">lambda</span> <span class="n">e</span><span class="p">,</span> <span class="n">v</span><span class="p">:</span> <span class="n">views</span><span class="p">[</span><span class="n">e</span><span class="p">](</span><span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">v</span><span class="p">),</span>
<span class="n">catch_http_exceptions</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Keep in mind that this method might return exception objects, too, so
use <tt class="xref py py-class docutils literal"><span class="pre">Response.force_type</span></tt> to get a response object.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>view_func</strong> – a function that is called with the endpoint as
first argument and the value dict as second. Has
to dispatch to the actual view function with this
information. (see above)</li>
<li><strong>path_info</strong> – the path info to use for matching. Overrides the
path info specified on binding.</li>
<li><strong>method</strong> – the HTTP method used for matching. Overrides the
method specified on binding.</li>
<li><strong>catch_http_exceptions</strong> – set to <cite>True</cite> to catch any of the
werkzeug <tt class="xref py py-class docutils literal"><span class="pre">HTTPException</span></tt>s.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.get_default_redirect">
<tt class="descname">get_default_redirect</tt><big>(</big><em>rule</em>, <em>method</em>, <em>values</em>, <em>query_args</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.get_default_redirect" title="Permalink to this definition">¶</a></dt>
<dd><p>A helper that returns the URL to redirect to if it finds one.
This is used for default redirecting only.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Internal:</th><td class="field-body"></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.get_host">
<tt class="descname">get_host</tt><big>(</big><em>domain_part</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.get_host" title="Permalink to this definition">¶</a></dt>
<dd><p>Figures out the full host name for the given domain part. The
domain part is a subdomain in case host matching is disabled or
a full host name.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.make_alias_redirect_url">
<tt class="descname">make_alias_redirect_url</tt><big>(</big><em>path</em>, <em>endpoint</em>, <em>values</em>, <em>method</em>, <em>query_args</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.make_alias_redirect_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Internally called to make an alias redirect URL.</p>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.make_redirect_url">
<tt class="descname">make_redirect_url</tt><big>(</big><em>path_info</em>, <em>query_args=None</em>, <em>domain_part=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.make_redirect_url" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates a redirect URL.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Internal:</th><td class="field-body"></td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.match">
<tt class="descname">match</tt><big>(</big><em>path_info=None</em>, <em>method=None</em>, <em>return_rule=False</em>, <em>query_args=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.match" title="Permalink to this definition">¶</a></dt>
<dd><p>The usage is simple: you just pass the match method the current
path info as well as the method (which defaults to <cite>GET</cite>). The
following things can then happen:</p>
<ul class="simple">
<li>you receive a <cite>NotFound</cite> exception that indicates that no URL is
matching. A <cite>NotFound</cite> exception is also a WSGI application you
can call to get a default page not found page (happens to be the
same object as <cite>werkzeug.exceptions.NotFound</cite>)</li>
<li>you receive a <cite>MethodNotAllowed</cite> exception that indicates that there
is a match for this URL but not for the current request method.
This is useful for RESTful applications.</li>
<li>you receive a <cite>RequestRedirect</cite> exception with a <cite>new_url</cite>
attribute. This exception is used to notify you about a request
Werkzeug requests from your WSGI application. This is for example the
case if you request <tt class="docutils literal"><span class="pre">/foo</span></tt> although the correct URL is <tt class="docutils literal"><span class="pre">/foo/</span></tt>
You can use the <cite>RequestRedirect</cite> instance as response-like object
similar to all other subclasses of <cite>HTTPException</cite>.</li>
<li>you get a tuple in the form <tt class="docutils literal"><span class="pre">(endpoint,</span> <span class="pre">arguments)</span></tt> if there is
a match (unless <cite>return_rule</cite> is True, in which case you get a tuple
in the form <tt class="docutils literal"><span class="pre">(rule,</span> <span class="pre">arguments)</span></tt>)</li>
</ul>
<p>If the path info is not passed to the match method the default path
info of the map is used (defaults to the root URL if not defined
explicitly).</p>
<p>All of the exceptions raised are subclasses of <cite>HTTPException</cite> so they
can be used as WSGI responses. The will all render generic error or
redirect pages.</p>
<p>Here is a small example for matching:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">m</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="gp">... </span> <span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Rule</span><span class="p">(</span><span class="s">'/downloads/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'downloads/index'</span><span class="p">),</span>
<span class="gp">... </span> <span class="n">Rule</span><span class="p">(</span><span class="s">'/downloads/<int:id>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'downloads/show'</span><span class="p">)</span>
<span class="gp">... </span><span class="p">])</span>
<span class="gp">>>> </span><span class="n">urls</span> <span class="o">=</span> <span class="n">m</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s">"example.com"</span><span class="p">,</span> <span class="s">"/"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">"/"</span><span class="p">,</span> <span class="s">"GET"</span><span class="p">)</span>
<span class="go">('index', {})</span>
<span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">"/downloads/42"</span><span class="p">)</span>
<span class="go">('downloads/show', {'id': 42})</span>
</pre></div>
</div>
<p>And here is what happens on redirect and missing URLs:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">"/downloads"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">RequestRedirect</span>: <span class="n">http://example.com/downloads/</span>
<span class="gp">>>> </span><span class="n">urls</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="s">"/missing"</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">NotFound</span>: <span class="n">404 Not Found</span>
</pre></div>
</div>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>path_info</strong> – the path info to use for matching. Overrides the
path info specified on binding.</li>
<li><strong>method</strong> – the HTTP method used for matching. Overrides the
method specified on binding.</li>
<li><strong>return_rule</strong> – return the rule that matched instead of just the
endpoint (defaults to <cite>False</cite>).</li>
<li><strong>query_args</strong> – optional query arguments that are used for
automatic redirects as string or dictionary. It’s
currently not possible to use the query arguments
for URL matching.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.6: </span><cite>return_rule</cite> was added.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7: </span><cite>query_args</cite> was added.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 0.8: </span><cite>query_args</cite> can now also be a string.</p>
</div>
</dd></dl>
<dl class="method">
<dt id="werkzeug.routing.MapAdapter.test">
<tt class="descname">test</tt><big>(</big><em>path_info=None</em>, <em>method=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.MapAdapter.test" title="Permalink to this definition">¶</a></dt>
<dd><p>Test if a rule would match. Works like <cite>match</cite> but returns <cite>True</cite>
if the URL matches, or <cite>False</cite> if it does not exist.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>path_info</strong> – the path info to use for matching. Overrides the
path info specified on binding.</li>
<li><strong>method</strong> – the HTTP method used for matching. Overrides the
method specified on binding.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.Rule">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">Rule</tt><big>(</big><em>string</em>, <em>defaults=None</em>, <em>subdomain=None</em>, <em>methods=None</em>, <em>build_only=False</em>, <em>endpoint=None</em>, <em>strict_slashes=None</em>, <em>redirect_to=None</em>, <em>alias=False</em>, <em>host=None</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Rule" title="Permalink to this definition">¶</a></dt>
<dd><p>A Rule represents one URL pattern. There are some options for <cite>Rule</cite>
that change the way it behaves and are passed to the <cite>Rule</cite> constructor.
Note that besides the rule-string all arguments <em>must</em> be keyword arguments
in order to not break the application on Werkzeug upgrades.</p>
<dl class="docutils">
<dt><cite>string</cite></dt>
<dd><p class="first">Rule strings basically are just normal URL paths with placeholders in
the format <tt class="docutils literal"><span class="pre"><converter(arguments):name></span></tt> where the converter and the
arguments are optional. If no converter is defined the <cite>default</cite>
converter is used which means <cite>string</cite> in the normal configuration.</p>
<p>URL rules that end with a slash are branch URLs, others are leaves.
If you have <cite>strict_slashes</cite> enabled (which is the default), all
branch URLs that are matched without a trailing slash will trigger a
redirect to the same URL with the missing slash appended.</p>
<p class="last">The converters are defined on the <cite>Map</cite>.</p>
</dd>
<dt><cite>endpoint</cite></dt>
<dd>The endpoint for this rule. This can be anything. A reference to a
function, a string, a number etc. The preferred way is using a string
because the endpoint is used for URL generation.</dd>
<dt><cite>defaults</cite></dt>
<dd><p class="first">An optional dict with defaults for other rules with the same endpoint.
This is a bit tricky but useful if you want to have unique URLs:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/all/'</span><span class="p">,</span> <span class="n">defaults</span><span class="o">=</span><span class="p">{</span><span class="s">'page'</span><span class="p">:</span> <span class="mi">1</span><span class="p">},</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'all_entries'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/all/page/<int:page>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'all_entries'</span><span class="p">)</span>
<span class="p">])</span>
</pre></div>
</div>
<p class="last">If a user now visits <tt class="docutils literal"><span class="pre">http://example.com/all/page/1</span></tt> he will be
redirected to <tt class="docutils literal"><span class="pre">http://example.com/all/</span></tt>. If <cite>redirect_defaults</cite> is
disabled on the <cite>Map</cite> instance this will only affect the URL
generation.</p>
</dd>
<dt><cite>subdomain</cite></dt>
<dd><p class="first">The subdomain rule string for this rule. If not specified the rule
only matches for the <cite>default_subdomain</cite> of the map. If the map is
not bound to a subdomain this feature is disabled.</p>
<p>Can be useful if you want to have user profiles on different subdomains
and all subdomains are forwarded to your application:</p>
<div class="last highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">subdomain</span><span class="o">=</span><span class="s">'<username>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'user/homepage'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/stats'</span><span class="p">,</span> <span class="n">subdomain</span><span class="o">=</span><span class="s">'<username>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'user/stats'</span><span class="p">)</span>
<span class="p">])</span>
</pre></div>
</div>
</dd>
<dt><cite>methods</cite></dt>
<dd><p class="first">A sequence of http methods this rule applies to. If not specified, all
methods are allowed. For example this can be useful if you want different
endpoints for <cite>POST</cite> and <cite>GET</cite>. If methods are defined and the path
matches but the method matched against is not in this list or in the
list of another rule for that path the error raised is of the type
<cite>MethodNotAllowed</cite> rather than <cite>NotFound</cite>. If <cite>GET</cite> is present in the
list of methods and <cite>HEAD</cite> is not, <cite>HEAD</cite> is added automatically.</p>
<div class="last versionchanged">
<p><span class="versionmodified">Changed in version 0.6.1: </span><cite>HEAD</cite> is now automatically added to the methods if <cite>GET</cite> is
present. The reason for this is that existing code often did not
work properly in servers not rewriting <cite>HEAD</cite> to <cite>GET</cite>
automatically and it was not documented how <cite>HEAD</cite> should be
treated. This was considered a bug in Werkzeug because of that.</p>
</div>
</dd>
<dt><cite>strict_slashes</cite></dt>
<dd>Override the <cite>Map</cite> setting for <cite>strict_slashes</cite> only for this rule. If
not specified the <cite>Map</cite> setting is used.</dd>
<dt><cite>build_only</cite></dt>
<dd>Set this to True and the rule will never match but will create a URL
that can be build. This is useful if you have resources on a subdomain
or folder that are not handled by the WSGI application (like static data)</dd>
<dt><cite>redirect_to</cite></dt>
<dd><p class="first">If given this must be either a string or callable. In case of a
callable it’s called with the url adapter that triggered the match and
the values of the URL as keyword arguments and has to return the target
for the redirect, otherwise it has to be a string with placeholders in
rule syntax:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">foo_with_slug</span><span class="p">(</span><span class="n">adapter</span><span class="p">,</span> <span class="nb">id</span><span class="p">):</span>
<span class="c"># ask the database for the slug for the old id. this of</span>
<span class="c"># course has nothing to do with werkzeug.</span>
<span class="k">return</span> <span class="s">'foo/'</span> <span class="o">+</span> <span class="n">Foo</span><span class="o">.</span><span class="n">get_slug_for_id</span><span class="p">(</span><span class="nb">id</span><span class="p">)</span>
<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/foo/<slug>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'foo'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/some/old/url/<slug>'</span><span class="p">,</span> <span class="n">redirect_to</span><span class="o">=</span><span class="s">'foo/<slug>'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/other/old/url/<int:id>'</span><span class="p">,</span> <span class="n">redirect_to</span><span class="o">=</span><span class="n">foo_with_slug</span><span class="p">)</span>
<span class="p">])</span>
</pre></div>
</div>
<p>When the rule is matched the routing system will raise a
<cite>RequestRedirect</cite> exception with the target for the redirect.</p>
<p class="last">Keep in mind that the URL will be joined against the URL root of the
script so don’t use a leading slash on the target URL unless you
really mean root of that domain.</p>
</dd>
<dt><cite>alias</cite></dt>
<dd>If enabled this rule serves as an alias for another rule with the same
endpoint and arguments.</dd>
<dt><cite>host</cite></dt>
<dd>If provided and the URL map has host matching enabled this can be
used to provide a match rule for the whole host. This also means
that the subdomain feature is disabled.</dd>
</dl>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7: </span>The <cite>alias</cite> and <cite>host</cite> parameters were added.</p>
</div>
<dl class="method">
<dt id="werkzeug.routing.Rule.empty">
<tt class="descname">empty</tt><big>(</big><big>)</big><a class="headerlink" href="#werkzeug.routing.Rule.empty" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an unbound copy of this rule. This can be useful if you
want to reuse an already bound URL for another map.</p>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="rule-factories">
<h2>Rule Factories<a class="headerlink" href="#rule-factories" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.routing.RuleFactory">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">RuleFactory</tt><a class="headerlink" href="#werkzeug.routing.RuleFactory" title="Permalink to this definition">¶</a></dt>
<dd><p>As soon as you have more complex URL setups it’s a good idea to use rule
factories to avoid repetitive tasks. Some of them are builtin, others can
be added by subclassing <cite>RuleFactory</cite> and overriding <cite>get_rules</cite>.</p>
<dl class="method">
<dt id="werkzeug.routing.RuleFactory.get_rules">
<tt class="descname">get_rules</tt><big>(</big><em>map</em><big>)</big><a class="headerlink" href="#werkzeug.routing.RuleFactory.get_rules" title="Permalink to this definition">¶</a></dt>
<dd><p>Subclasses of <cite>RuleFactory</cite> have to override this method and return
an iterable of rules.</p>
</dd></dl>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.Subdomain">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">Subdomain</tt><big>(</big><em>subdomain</em>, <em>rules</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Subdomain" title="Permalink to this definition">¶</a></dt>
<dd><p>All URLs provided by this factory have the subdomain set to a
specific domain. For example if you want to use the subdomain for
the current language this can be a good setup:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'#select_language'</span><span class="p">),</span>
<span class="n">Subdomain</span><span class="p">(</span><span class="s">'<string(length=2):lang_code>'</span><span class="p">,</span> <span class="p">[</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/about'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'about'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/help'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'help'</span><span class="p">)</span>
<span class="p">])</span>
<span class="p">])</span>
</pre></div>
</div>
<p>All the rules except for the <tt class="docutils literal"><span class="pre">'#select_language'</span></tt> endpoint will now
listen on a two letter long subdomain that holds the language code
for the current request.</p>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.Submount">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">Submount</tt><big>(</big><em>path</em>, <em>rules</em><big>)</big><a class="headerlink" href="#werkzeug.routing.Submount" title="Permalink to this definition">¶</a></dt>
<dd><p>Like <cite>Subdomain</cite> but prefixes the URL rule with a given string:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">),</span>
<span class="n">Submount</span><span class="p">(</span><span class="s">'/blog'</span><span class="p">,</span> <span class="p">[</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/index'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/entry/<entry_slug>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'blog/show'</span><span class="p">)</span>
<span class="p">])</span>
<span class="p">])</span>
</pre></div>
</div>
<p>Now the rule <tt class="docutils literal"><span class="pre">'blog/show'</span></tt> matches <tt class="docutils literal"><span class="pre">/blog/entry/<entry_slug></span></tt>.</p>
</dd></dl>
<dl class="class">
<dt id="werkzeug.routing.EndpointPrefix">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">EndpointPrefix</tt><big>(</big><em>prefix</em>, <em>rules</em><big>)</big><a class="headerlink" href="#werkzeug.routing.EndpointPrefix" title="Permalink to this definition">¶</a></dt>
<dd><p>Prefixes all endpoints (which must be strings for this factory) with
another string. This can be useful for sub applications:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">),</span>
<span class="n">EndpointPrefix</span><span class="p">(</span><span class="s">'blog/'</span><span class="p">,</span> <span class="p">[</span><span class="n">Submount</span><span class="p">(</span><span class="s">'/blog'</span><span class="p">,</span> <span class="p">[</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'index'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/entry/<entry_slug>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'show'</span><span class="p">)</span>
<span class="p">])])</span>
<span class="p">])</span>
</pre></div>
</div>
</dd></dl>
</div>
<div class="section" id="rule-templates">
<h2>Rule Templates<a class="headerlink" href="#rule-templates" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="werkzeug.routing.RuleTemplate">
<em class="property">class </em><tt class="descclassname">werkzeug.routing.</tt><tt class="descname">RuleTemplate</tt><big>(</big><em>rules</em><big>)</big><a class="headerlink" href="#werkzeug.routing.RuleTemplate" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns copies of the rules wrapped and expands string templates in
the endpoint, rule, defaults or subdomain sections.</p>
<p>Here a small example for such a rule template:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="kn">import</span> <span class="n">Map</span><span class="p">,</span> <span class="n">Rule</span><span class="p">,</span> <span class="n">RuleTemplate</span>
<span class="n">resource</span> <span class="o">=</span> <span class="n">RuleTemplate</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/$name/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'$name.list'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/$name/<int:id>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'$name.show'</span><span class="p">)</span>
<span class="p">])</span>
<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span><span class="n">resource</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'user'</span><span class="p">),</span> <span class="n">resource</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">'page'</span><span class="p">)])</span>
</pre></div>
</div>
<p>When a rule template is called the keyword arguments are used to
replace the placeholders in all the string parameters.</p>
</dd></dl>
</div>
<div class="section" id="custom-converters">
<h2>Custom Converters<a class="headerlink" href="#custom-converters" title="Permalink to this headline">¶</a></h2>
<p>You can easily add custom converters. The only thing you have to do is to
subclass <tt class="xref py py-class docutils literal"><span class="pre">BaseConverter</span></tt> and pass that new converter to the url_map.
A converter has to provide two public methods: <cite>to_python</cite> and <cite>to_url</cite>,
as well as a member that represents a regular expression. Here is a small
example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">random</span> <span class="kn">import</span> <span class="n">randrange</span>
<span class="kn">from</span> <span class="nn">werkzeug.routing</span> <span class="kn">import</span> <span class="n">Rule</span><span class="p">,</span> <span class="n">Map</span><span class="p">,</span> <span class="n">BaseConverter</span><span class="p">,</span> <span class="n">ValidationError</span>
<span class="k">class</span> <span class="nc">BooleanConverter</span><span class="p">(</span><span class="n">BaseConverter</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">url_map</span><span class="p">,</span> <span class="n">randomify</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
<span class="nb">super</span><span class="p">(</span><span class="n">BooleanConverter</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">__init__</span><span class="p">(</span><span class="n">url_map</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">randomify</span> <span class="o">=</span> <span class="n">randomify</span>
<span class="bp">self</span><span class="o">.</span><span class="n">regex</span> <span class="o">=</span> <span class="s">'(?:yes|no|maybe)'</span>
<span class="k">def</span> <span class="nf">to_python</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">if</span> <span class="n">value</span> <span class="o">==</span> <span class="s">'maybe'</span><span class="p">:</span>
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">randomify</span><span class="p">:</span>
<span class="k">return</span> <span class="ow">not</span> <span class="n">randrange</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
<span class="k">raise</span> <span class="n">ValidationError</span><span class="p">()</span>
<span class="k">return</span> <span class="n">value</span> <span class="o">==</span> <span class="s">'yes'</span>
<span class="k">def</span> <span class="nf">to_url</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
<span class="k">return</span> <span class="n">value</span> <span class="ow">and</span> <span class="s">'yes'</span> <span class="ow">or</span> <span class="s">'no'</span>
<span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/vote/<bool:werkzeug_rocks>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'vote'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/vote/<bool(randomify=True):foo>'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'foo'</span><span class="p">)</span>
<span class="p">],</span> <span class="n">converters</span><span class="o">=</span><span class="p">{</span><span class="s">'bool'</span><span class="p">:</span> <span class="n">BooleanConverter</span><span class="p">})</span>
</pre></div>
</div>
<p>If you want that converter to be the default converter, name it <tt class="docutils literal"><span class="pre">'default'</span></tt>.</p>
</div>
<div class="section" id="host-matching">
<h2>Host Matching<a class="headerlink" href="#host-matching" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 0.7.</span></p>
</div>
<p>Starting with Werkzeug 0.7 it’s also possible to do matching on the whole
host names instead of just the subdomain. To enable this feature you need
to pass <tt class="docutils literal"><span class="pre">host_matching=True</span></tt> to the <a class="reference internal" href="#werkzeug.routing.Map" title="werkzeug.routing.Map"><tt class="xref py py-class docutils literal"><span class="pre">Map</span></tt></a> constructor and provide
the <cite>host</cite> argument to all routes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'www_index'</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">'www.example.com'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'help_index'</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">'help.example.com'</span><span class="p">)</span>
<span class="p">],</span> <span class="n">host_matching</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>Variable parts are of course also possible in the host section:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url_map</span> <span class="o">=</span> <span class="n">Map</span><span class="p">([</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'www_index'</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">'www.example.com'</span><span class="p">),</span>
<span class="n">Rule</span><span class="p">(</span><span class="s">'/'</span><span class="p">,</span> <span class="n">endpoint</span><span class="o">=</span><span class="s">'user_index'</span><span class="p">,</span> <span class="n">host</span><span class="o">=</span><span class="s">'<user>.example.com'</span><span class="p">)</span>
<span class="p">],</span> <span class="n">host_matching</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper"><p class="logo"><a href="index.html">
<img class="logo" src="_static/werkzeug.png" alt="Logo"/>
</a></p>
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">URL Routing</a><ul>
<li><a class="reference internal" href="#quickstart">Quickstart</a></li>
<li><a class="reference internal" href="#rule-format">Rule Format</a></li>
<li><a class="reference internal" href="#builtin-converters">Builtin Converters</a></li>
<li><a class="reference internal" href="#maps-rules-and-adapters">Maps, Rules and Adapters</a></li>
<li><a class="reference internal" href="#rule-factories">Rule Factories</a></li>
<li><a class="reference internal" href="#rule-templates">Rule Templates</a></li>
<li><a class="reference internal" href="#custom-converters">Custom Converters</a></li>
<li><a class="reference internal" href="#host-matching">Host Matching</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="index.html">Documentation overview</a><ul>
<li>Previous: <a href="wrappers.html" title="previous chapter">Request / Response Objects</a></li>
<li>Next: <a href="wsgi.html" title="next chapter">WSGI Helpers</a></li>
</ul></li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/routing.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2011, The Werkzeug Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|