Right method for url mapping

14 views
Skip to first unread message

Saeed Pooladzadeh

unread,
May 25, 2019, 7:14:18 PM5/25/19
to Django users
Hello

Wich of them is the right method for url mapping:

  path('', views.show, name='index'),

or

path('index', views.show),

Regards,

Saeed

Yoo

unread,
May 25, 2019, 7:19:59 PM5/25/19
to Django users
First one. Usually I'd have it be views.index. Otherwise, the first one is perfectly fine. Need explanation?

Saeed Pooladzadeh

unread,
May 26, 2019, 6:41:03 AM5/26/19
to Django users
Can you please explain. But I have seen in one of my app it works with just second one!!

در یکشنبه 26 مهٔ 2019، ساعت 3:49:59 (UTC+4:30)، Yoo نوشته:

Yoo

unread,
May 26, 2019, 7:25:55 AM5/26/19
to Django users
Are you in your application's urls.py or your project's urls.py? You need to be in your APPLICATION'S urls.py for this to work. Your application's folder has admin.py, apps.py, forms.py, etc. 

In your views.py, do you have "show" defined? Like:
def show(request):
   context
= {}
   render
(request, 'index.html', context=context)

Here's the explanation:

I still recommend you do something like this in your urls.py:
from django.urls import path
from . import views
path
('', views.index, name='index'),
This is just to make everything less confusing. The name, 'index,' is used in your templates. For example, if you have:
<p><a href="{% url 'index' %}">Home Page</a></p>
This is a  hyperlink to the home page called "index." 
The views.index means "use this view called index" like so: 
def index(request):
   context
={}
   render
(request, 'index.html', context=context)
I'm assuming you're using a template called "index.html" to load everything in.
I'd recommend you go to Mozilla's Django tutorial to learn a bit. They have some good explanations with phenomenal tutorials.

Does that make sense?

avi gehlot

unread,
May 26, 2019, 7:48:05 AM5/26/19
to Django users
you can write only 
path(' ', views.show, name='index')

Saeed Pooladzadeh

unread,
May 26, 2019, 9:13:53 AM5/26/19
to Django users
Yes, that's very fine. Thanx

در یکشنبه 26 مهٔ 2019، ساعت 15:55:55 (UTC+4:30)، Yoo نوشته:
Reply all
Reply to author
Forward
0 new messages