gpt4 book ai didi

python - django - urls.py 根路径集

转载 作者:太空宇宙 更新时间:2023-11-03 14:14:26 25 4
gpt4 key购买 nike

当我输入 localhost:8000localhost:8000/store 时,两个 View 都与 home/index.html 相同。我希望这些路径设置不同的 View 。

网址.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 - 主页

from django.shortcuts import render

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

views.py - 商店

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,
}
)

干杯!

编辑:

我也尝试了 url(r'^$', 'home.views.index', name='view_home'),,但结果相同。

最佳答案

您需要锚定和终止 Root View 的模式:

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

关于python - django - urls.py 根路径集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com