Bhargav Kothadia
Bhargav Kothadia

Reputation: 11

How To use multiple url with the same template file in django.?

URLS.PY

url(r'^shop.html$', views.alldress, name='alldress'),
url(r'^shop.html$', views.cart ,name='cart'),
url(r'^shop.html$', views.cartdata, name='cartdata'),

I want to use three process with the same time so How To use multiple url with the same template file in django.?

Upvotes: 1

Views: 3168

Answers (1)

David Foulke Jr
David Foulke Jr

Reputation: 98

In Django, the urls.py file is meant to connect URL patterns with a view function. If you have multiple URL patterns that are meant to do the same thing, just connect them all to the same view function, like so:

url(r'^shop/$', views.shop, name='shop'),
url(r'^cart/$', views.shop, name='cart'),
url(r'^cartdata/$', views.shop, name='cartdata')

If you want your URL patterns to do different things but still render the same template, you can just render that template in each of your views.py functions.

# urls.py
url(r'^shop/$', views.shop, name='shop'),
url(r'^cart/$', views.cart, name='cart'),
url(r'^cartdata/$', views.cartdata, name='cartdata')

# views.py
def shop(request):
    context = {}
    ...
    return render(request, 'shop.html', context)

def cart(request):
    context = {}
    ...
    return render(request, 'shop.html', context)

def cartdata(request):
    context = {}
    ...
    return render(request, 'shop.html', context)

In this example, each of the three views.py functions perform different operations, but they all render the same HTML file, shop.html.

Upvotes: 3

Related Questions