在一个名为batches的应用程序中,我设置了不同的url模式。我现在有3个批量,个人和商业名称的网址。似乎在goind到最后2时我只是一直在看批量信息。在我的app-batch中,我有3个表/类。
请参阅我的网站网址模式:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^batches/', include('batches.urls')),
url(r'^screenings/', include('screenings.urls')),
url(r'^individuals/', include('batches.urls')),
url(r'^businessnames', include('batches.urls')),
]
这就是我的观点:
from __future__ import unicode_literals
from .models import BusinessName
from .models import Individuals
from .models import Batches
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
all_Batches = Batches.objects.all()
html = ''
for batch in all_Batches:
url = '/batches/' + str(batch.id) + '/'
html += '<a href="#"' + url + '">' + str(batch.FileName)+ '</a><br>'
return HttpResponse(html)
def detail(request, batches_id): #parametrul asta batches_id tr sa se gaseasca in urls.py din app
return HttpResponse("<h2>Details for Batches ID:" + str(batches_id) + "</h2")
def index_businessname(request):
all_BusinessNames = BusinessName.objects.all()
html1 = ''
for bn in all_BusinessNames:
url = '/businessnames/' + str(bn.id) + '/'
html1 += '<a href="#"' + url + '">' + bn.FullName + '</a><br>'
return HttpResponse(html1)
def detail_businessname(request, businessname_id):
return HttpResponse("<h2>Details for Business Names ID:" + str(businessname_id) + "</h2")
def index_individual(request):
all_individuals = Individuals.objects.all()
html2 = ''
for i in all_individuals:
url = '/individuals/' + i.id + '/'
html2 += '<a href="#"' + url + '">' + i.FullName + '</a><br>'
return HttpResponse(html2)
def detail_individual(request, individuals_id):
return HttpResponse("<h2>Details for Individual Names ID:" + str(individuals_id)+ "</h2")
这就是我在我的应用网址中的内容:
urlpatterns = [
# /batches/
url(r'^$', views.index, name='index'),
# /batches/2
url(r'^(?P<batches_id>[0-9]+)/$',views.detail, name="detail"),
# businessname/1
url(r'^$',views.index_businessname, name="index_businessname"),
# businessname/1
url(r'^(?P<businessname_id>[0-9]+)/$', views.detail_businessname, name="detail_businessname"),
# individuals/1
url(r'^$', views.index_individual, name="index_individuals"),
# individuals/1
url(r'^(?P<individuals_id>[0-9]+)/$', views.detail_individual, name="detail_individual"),
]
如果有人可以帮助我,我会欠你很多。我阅读了文档,但我被困在其中。
谢谢你, 科恩
答案 0 :(得分:2)
问题是您要为同一个网址定义三个端点。 Django从上到下搜索模式并返回首先匹配的模式。因此,除非您明确说明,否则它不知道您指定了哪一个。因此,最好在相同的应用程序本身中为应用程序中的URL定义端点的前缀。
您可以在主urls.py
,
url(r'^batches/', include('batches.urls')),
然后,在app.urls.py
相应地更改它们,
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^/(?P<batches_id>[0-9]+)/$',views.detail, name="detail"),
url(r'^businessname/$',views.index_businessname, name="index_businessname"),
url(r'^businessname/(?P<businessname_id>[0-9]+)/$', views.detail_businessname, name="detail_businessname"),
url(r'^individuals/$', views.index_individual, name="index_individuals"),
url(r'^individuals/(?P<individuals_id>[0-9]+)/$', views.detail_individual, name="detail_individual"),
]