App Name

51 views
Skip to first unread message

Aniket Aryamane

unread,
Jul 11, 2018, 9:56:02 PM7/11/18
to Django developers (Contributions to Django itself)
Hello,

If in the urls.py, I can write:
app_name = 'posts'
.
.
path(' ', views.home, name='home'),



then why it is required to refer url name (from the template) by the app_name value as:
{% url 'posts:home' %}

It should be referred instead by app_name variable like below:
{% url 'app_name:home' %}


What do you guys think?



Thanks,
Aniket.

ludovic coues

unread,
Jul 12, 2018, 2:33:44 AM7/12/18
to django-d...@googlegroups.com
That question should be posted on Django user, where people discuss on how to use django to build web site and web application. This list is for improving djnago.

Anyway.
The first argument of the url tag is the name of the URL you want. The most common way to specify an URL name is with the format "{app}:{url}". This let you have each app have an home view without conflict and you can still can fetch the URL from the right app.

The name of an app is always stored in a variable app_name. And URL won't alter the URL name you gave it. So if you ask for 'app_name:posts', you will get the URL named posts from the app with a variable app_name set to the value app_name.


Maybe you are thinking of relative URL name resolving, which would be an improvement to Django. What I mean is the url tag would recognize a special value as meaning "fetch that view from the current app".

In my opinion, either a blank app name or 'self' would be a better choice for that special value. And it would cause a lot of issue. It might break existing Django app, which is bad. It will make finding all references if an URL in you code harder as you need to search for both the absolute and the relative name version.


I hope this answer your questions and thanks for asking


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To post to this group, send email to django-d...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-developers.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/ce6c2ef7-b463-43d5-bd57-5f2ca3d6fda6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.a

Franklin Sarmiento

unread,
Jul 12, 2018, 7:22:47 AM7/12/18
to django-d...@googlegroups.com
Hi bro, you can use this 

{% url 'home' %}

the app_name isn't the namespace, the namespace is define on the main urls.py, if you haven't a namespace defined, so, with the name assined on the path() is sufficient

greetings!

____________________________________________________
Franklin Sarmiento
Full-stack developer
Twitter: @franklinitiel
linkedin: Franklin Sarmiento ( frankl...@gmail.com )
Teléfono(s): +57 320 490.79.64 / +58 426 273.8103 ( whatsapp )



--
Reply all
Reply to author
Forward
0 new messages