python – django – urls.py根路径集
作者:互联网
当我输入localhost:8000或localhost:8000 / store时,两个视图都与home / index.html相同.
我希望这些路径设置不同的视图.
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 – 家
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’),但它带来了相同的结果.
解决方法:
我认为你的模板名称相互冲突.您可以像这样为整个项目创建一个模板文件夹
Project
|>Templates
|>store
|>home
在模板中,您可以像这样放置模板
Project
|>Templates
store
|>index.html
home
|>index.html
|store
|home
现在,您可以像这样给出模板的路径
def index(request):
return render(
request,
'home/index.html'
)
如果您想将模板保存在您的应用程序下的目录中,那么this问题可能会对您有所帮助.
标签:python,django,django-urls 来源: https://codeday.me/bug/20190829/1761071.html