| 501 | | **New in development version** |
|---|
| 502 | | |
|---|
| 503 | | It is fairly common to use the same view function in multiple URL patterns in |
|---|
| 504 | | your URLConf. This leads to problems when you come to do reverse URL matching, |
|---|
| 505 | | because the ``permalink()`` decorator and ``{% url %}`` template tag use the |
|---|
| 506 | | name of the view function to find a match. |
|---|
| 507 | | |
|---|
| 508 | | To solve this problem, you can give a name to each of your URL patterns in |
|---|
| 509 | | order to distinguish them from other patterns using the same views and |
|---|
| 510 | | parameters. You can then use this name wherever you would otherwise use the |
|---|
| 511 | | name of the view function. For example, if you URLConf contains:: |
|---|
| | 502 | **New in Django development version** |
|---|
| | 503 | |
|---|
| | 504 | It's fairly common to use the same view function in multiple URL patterns in |
|---|
| | 505 | your URLconf. For example, these two URL patterns both point to the ``archive`` |
|---|
| | 506 | view:: |
|---|
| | 507 | |
|---|
| | 508 | urlpatterns = patterns('', |
|---|
| | 509 | (r'/archive/(\d{4})/$', archive), |
|---|
| | 510 | (r'/archive-summary/(\d{4})/$', archive, {'summary': True}), |
|---|
| | 511 | ) |
|---|
| | 512 | |
|---|
| | 513 | This is completely valid, but it leads to problems when you try to do reverse |
|---|
| | 514 | URL matching (through the ``permalink()`` decorator or the ``{% url %}`` |
|---|
| | 515 | template tag). Continuing this example, if you wanted to retrieve the URL for |
|---|
| | 516 | the ``archive`` view, Django's reverse URL matcher would get confused, because |
|---|
| | 517 | *two* URLpatterns point at that view. |
|---|
| | 518 | |
|---|
| | 519 | To solve this problem, Django supports **named URL patterns**. That is, you can |
|---|
| | 520 | give a name to a URL pattern in order to distinguish it from other patterns |
|---|
| | 521 | using the same view and parameters. Then, you can use this name in reverse URL |
|---|
| | 522 | matching. |
|---|
| | 523 | |
|---|
| | 524 | Here's the above example, rewritten to used named URL patterns:: |
|---|
| 530 | | Make sure that when you name your URLs, you use names that are unlikely to |
|---|
| 531 | | clash with any other application's choice of names. If you call your URL |
|---|
| 532 | | pattern *comment* and another application does the same thing, there is no |
|---|
| 533 | | guarantee which URL will be inserted into your template when you use this |
|---|
| 534 | | name. Putting a prefix on your URL names, perhaps derived from |
|---|
| 535 | | the application name, will decrease the chances of collision. Something |
|---|
| 536 | | like *myapp-comment* is recommended over simply *comment*. |
|---|
| 537 | | |
|---|
| | 545 | When you name your URL patterns, make sure you use names that are unlikely |
|---|
| | 546 | to clash with any other application's choice of names. If you call your URL |
|---|
| | 547 | pattern ``comment``, and another application does the same thing, there's |
|---|
| | 548 | no guarantee which URL will be inserted into your template when you use |
|---|
| | 549 | this name. |
|---|
| | 550 | |
|---|
| | 551 | Putting a prefix on your URL names, perhaps derived from the application |
|---|
| | 552 | name, will decrease the chances of collision. We recommend something like |
|---|
| | 553 | ``myapp-comment`` instead of ``comment``. |
|---|