Skip to content
Advertisement

How to create a url for two apps that have index function in django?

I am a newbie in Django. In one django project, I have two apps say college app and company app. In the college app.

college/urls.py

path('',index,name="index")

Company/urls.py

path('company/', HomeView.as_view(), name="index")

How do I create a url of both these in the header.html ? I tried these but its not working.

<li>
    <a href="{% url 'index' %}"> College Home Page ||</a>`
    <a href="{% url 'company/index' %}"> Company Home Page ||</a>
</li>

Advertisement

Answer

There is a easier way to about this conflict just by using different name in URLS.

path('company/', HomeView.as_view(), name="company_index")
or,
path('', index, name="college_index")

But if you still want to use “Index” as URL name then you should consider using app name before the URLS name.

for example, in template urls or redirect in view you can added app name before URL

{% url 'company:index' %}
{% url 'college:index' %}
or,
redirect('company:index')
redirect('college:index')
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement