Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
D
django
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
django
Commits
98bcc5d8
Kaydet (Commit)
98bcc5d8
authored
Kas 05, 2016
tarafından
Robert Roskam
Kaydeden (comit)
Tim Graham
Şub 11, 2017
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fixed #27367 -- Doc'd and tested reversing of URLs with the same name.
Thanks Reinout van Rees for contributing to the patch.
üst
fb5bd38e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
54 additions
and
7 deletions
+54
-7
urls.txt
docs/topics/http/urls.txt
+20
-7
named_urls_conflict.py
tests/urlpatterns_reverse/named_urls_conflict.py
+17
-0
tests.py
tests/urlpatterns_reverse/tests.py
+17
-0
No files found.
docs/topics/http/urls.txt
Dosyayı görüntüle @
98bcc5d8
...
...
@@ -597,15 +597,28 @@ In order to perform URL reversing, you'll need to use **named URL patterns**
as done in the examples above. The string used for the URL name can contain any
characters you like. You are not restricted to valid Python names.
When
you name your URL patterns, make sure you use names that are unlikely
to clash with any other application's choice of names. If you call your URL
pattern ``comment``, and another application does the same thing, there's
no guarantee which URL will be inserted into your template when you use
this name
.
When
naming URL patterns, choose names that are unlikely to clash with other
applications' choice of names. If you call your URL pattern ``comment``
and another application does the same thing, the URL that
:func:`~django.urls.reverse()` finds depends on whichever pattern is last in
your project's ``urlpatterns`` list
.
Putting a prefix on your URL names, perhaps derived from the application
name, will decrease the chances of collision. We recommend something like
``myapp-comment`` instead of ``comment``.
name (such as ``myapp-comment`` instead of ``comment``), decreases the chance
of collision.
You can deliberately choose the *same URL name* as another application if you
want to override a view. For example, a common use case is to override the
:class:`~django.contrib.auth.views.LoginView`. Parts of Django and most
third-party apps assume that this view has a URL pattern with the name
``login``. If you have a custom login view and give its URL the name ``login``,
:func:`~django.urls.reverse()` will find your custom view as long as it's in
``urlpatterns`` after ``django.contrib.auth.urls`` is included (if that's
included at all).
You may also use the same name for multiple URL patterns if they differ in
their arguments. In addition to the URL name, :func:`~django.urls.reverse()`
matches the number of arguments and the names of the keyword arguments.
.. _topics-http-defining-url-namespaces:
...
...
tests/urlpatterns_reverse/named_urls_conflict.py
0 → 100644
Dosyayı görüntüle @
98bcc5d8
from
django.conf.urls
import
url
from
.views
import
empty_view
urlpatterns
=
[
# No kwargs
url
(
r'^conflict/cannot-go-here/$'
,
empty_view
,
name
=
'name-conflict'
),
url
(
r'^conflict/$'
,
empty_view
,
name
=
'name-conflict'
),
# One kwarg
url
(
r'^conflict-first/(?P<first>\w+)/$'
,
empty_view
,
name
=
'name-conflict'
),
url
(
r'^conflict-cannot-go-here/(?P<middle>\w+)/$'
,
empty_view
,
name
=
'name-conflict'
),
url
(
r'^conflict-middle/(?P<middle>\w+)/$'
,
empty_view
,
name
=
'name-conflict'
),
url
(
r'^conflict-last/(?P<last>\w+)/$'
,
empty_view
,
name
=
'name-conflict'
),
# Two kwargs
url
(
r'^conflict/(?P<another>\w+)/(?P<extra>\w+)/cannot-go-here/$'
,
empty_view
,
name
=
'name-conflict'
),
url
(
r'^conflict/(?P<extra>\w+)/(?P<another>\w+)/$'
,
empty_view
,
name
=
'name-conflict'
),
]
tests/urlpatterns_reverse/tests.py
Dosyayı görüntüle @
98bcc5d8
...
...
@@ -386,6 +386,23 @@ class ResolverTests(SimpleTestCase):
self
.
assertEqual
(
resolver
.
reverse
(
'named-url2'
,
'arg'
),
'extra/arg/'
)
self
.
assertEqual
(
resolver
.
reverse
(
'named-url2'
,
extra
=
'arg'
),
'extra/arg/'
)
def
test_resolver_reverse_conflict
(
self
):
"""
url() name arguments don't need to be unique. The last registered
pattern takes precedence for conflicting names.
"""
resolver
=
get_resolver
(
'urlpatterns_reverse.named_urls_conflict'
)
# Without arguments, the last URL in urlpatterns has precedence.
self
.
assertEqual
(
resolver
.
reverse
(
'name-conflict'
),
'conflict/'
)
# With an arg, the last URL in urlpatterns has precedence.
self
.
assertEqual
(
resolver
.
reverse
(
'name-conflict'
,
'arg'
),
'conflict-last/arg/'
)
# With a kwarg, other url()s can be reversed.
self
.
assertEqual
(
resolver
.
reverse
(
'name-conflict'
,
first
=
'arg'
),
'conflict-first/arg/'
)
self
.
assertEqual
(
resolver
.
reverse
(
'name-conflict'
,
middle
=
'arg'
),
'conflict-middle/arg/'
)
self
.
assertEqual
(
resolver
.
reverse
(
'name-conflict'
,
last
=
'arg'
),
'conflict-last/arg/'
)
# The number and order of the arguments don't interfere with reversing.
self
.
assertEqual
(
resolver
.
reverse
(
'name-conflict'
,
'arg'
,
'arg'
),
'conflict/arg/arg/'
)
def
test_non_regex
(
self
):
"""
A Resolver404 is raised if resolving doesn't meet the basic
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment