[Django]-Django – urls.py root path set

1πŸ‘

βœ…

I think as your template name is conflicting with each other. You can make one templates folder for you whole project like this

Project
   |>Templates
   |>store
   |>home

In templates, you can put your templates like this

 Project
   |>Templates
        store
            |>index.html
        home
            |>index.html
   |store
   |home

Now you can give path of your template like this

def index(request):
return render(
    request,
    'home/index.html'
)

If you want to keep templates in directory under you app, then this issue might help you.

4πŸ‘

You need to anchor and terminate the pattern for the root view:

 url(r'^$', 'home.views.index', name='view_home'),

0πŸ‘

You have a trailing slash β€˜/’ in

url(r'^store/$', 'store.views.store_list', name='view_store_list')

so it will be valid for the url localhost:8000/store/ but not for the url localhost:8000/store. So you need to remove trailing slash.

Plus @Daniel is right you need the put the home URL like he mentioned in his answer. Trailing slash is the reason you could not get it to work after changing the home URL.

0πŸ‘

The first problem, as mentioned by Daniel is the url pattern, if you just set the empty string it will always match. Therefore you should use url(r'^$', 'home.views.index', name='view_home'). With r'^$' you are saying that the pattern match only with the empty string. Alternatively you can move this url at the end of the list and it will be used as the default view if anything else matches.

The second problem I see is that in your store_list view you are returning:

return render(
    request,
    'index.html',
    {
        'stores': stores,
    }
) 

Are you using the same template, index.html, for both index and store or is it just a copy and paste error? If so, are you sure that the stores variable contains data? If you don’t have any result the outcome will be the same as the index view.

πŸ‘€SimoV8

Leave a comment