简体   繁体   中英

django - urls.py root path set

When I enter localhost:8000 or localhost:8000/store , both views are same as home/index.html . I want these paths to set different view.

urls.py

from django.conf.urls import url
from django.contrib import admin

urlpatterns = [
    url(r'', 'home.views.index', name='view_home'),
    url(r'^admin/', admin.site.urls),
    url(r'^store/$', 'store.views.store_list', name='view_store_list'),
    url(r'^store/(?P<store_id>\d+)$', 'store.views.single_store')
]

views.py - home

from django.shortcuts import render

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

views.py - store

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse

from .models import Store # fetch Store model

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger # paginate
def store_list(request):
    store_list = Store.objects.all()
    paginator = Paginator(store_list, 2)

    page = request.GET.get('page')
    try:
        stores = paginator.page(page)
    except PageNotAnInteger:
        stores = paginator.page(1)
    except EmptyPage:
        stores = paginator.page(paginator.num_pages)

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

cheers!

edit:

I also tried url(r'^$', 'home.views.index', name='view_home'), , however it brings same result.

您需要锚定并终止根视图的模式:

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM