Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django视图--can';无法正确查看URL_Django - Fatal编程技术网

Django视图--can';无法正确查看URL

Django视图--can';无法正确查看URL,django,Django,在一个名为batches的应用程序中,我设置了不同的url模式。我现在有3个批量、个人和企业名称的URL。看起来,当我进入最后2页时,我一直只看到来自批次的信息。在我的应用程序批中,我有3个表/类 请查看我的网站url模式: urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^batches/', include('batches.urls')), url(r'^screenings/', include('s

在一个名为batches的应用程序中,我设置了不同的url模式。我现在有3个批量、个人和企业名称的URL。看起来,当我进入最后2页时,我一直只看到来自批次的信息。在我的应用程序批中,我有3个表/类

请查看我的网站url模式:

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")
从未来导入unicode文本
from.models导入BusinessName
从模型导入个人
从。模型导入批
从django.shortcuts导入渲染
从django.http导入HttpResponse
#在这里创建您的视图。
def索引(请求):
all_Batches=Batches.objects.all()
html=“”
对于所有批次中的批次:
url='/batches/'+str(batch.id)+'/'
html+='
' 返回HttpResponse(html) def详细信息(请求,批次id):#参数asta批次id tr sa se gaseasca在URL.py din应用程序中
返回HttpResponse(“批次ID的详细信息:+str(批次ID)+“问题是,您正在为相同的URL定义三个端点。Django从上到下搜索模式,并首先返回匹配的内容。因此,除非您明确告诉它,否则它不知道您指定了哪一个。因此,最好在同一个应用程序中定义URL端点的前缀

您可以在main
url.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"),
]
urlpatterns=[
url(r'^$',views.index,name='index'),
url(r'^/(?P[0-9]+)/$”,views.detail,name=“detail”),
url(r“^businessname/$”,views.index\u businessname,name=“index\u businessname”),
url(r“^businessname/(?P[0-9]+)/$”,views.detail\u businessname,name=“detail\u businessname”),
url(r“^individuals/$”,views.index\u individuals,name=“index\u individuals”),
url(r'^individuals/(?P[0-9]+)/$),views.detail_individual,name=“detail_individual”),
]

问题在于,您正在为相同的URL定义三个端点。Django从上到下搜索模式,并首先返回匹配的内容。因此,除非您明确告诉它,否则它不知道您指定了哪一个。因此,最好在同一个应用程序中定义URL端点的前缀

您可以在main
url.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"),
]
urlpatterns=[
url(r'^$',views.index,name='index'),
url(r'^/(?P[0-9]+)/$”,views.detail,name=“detail”),
url(r“^businessname/$”,views.index\u businessname,name=“index\u businessname”),
url(r“^businessname/(?P[0-9]+)/$”,views.detail\u businessname,name=“detail\u businessname”),
url(r“^individuals/$”,views.index\u individuals,name=“index\u individuals”),
url(r'^individuals/(?P[0-9]+)/$),views.detail_individual,name=“detail_individual”),
]

您希望Django如何知道批量请求和业务请求之间的区别?URL模式完全相同。好的,谢谢!我如何更改?最好将URL文件分开,在app文件夹中为批量、个人和业务创建3个单独的URL文件,并将URL从URL.py文件重定向到spec如果您希望在特定文件中附加功能,它将在将来对您有所帮助。您希望Django如何知道批处理请求和业务请求之间的区别?URL模式完全相同。好的,谢谢!我可以如何更改此设置?最好将URL文件分开,为批处理创建3个单独的URL文件s、 应用程序文件夹中的个人和企业,并将URL从URL.py文件重定向到特定文件,如果您希望在特定文件中附加功能,这将对您有所帮助谢谢zaidfazil的回答!因此,您建议更改我网站的URL模式,这2个:URL(r'^Persons/',include('Personal.URL')),URL(r“^businessnames”,include('businessnames.url')),No,保留
url(r“^”,include('batches.url'),
,然后删除所有重复项(即,其中包含
include('batches.url')
)在您的主
url.py
。然后更改模式,如我在
batches.url.py
中的回答。您好,zaidfazil,我更改了url模式。我还需要做什么?要像这样更改2个url==>url(r'^Personal/')、url(r'^BusinessName/'),您网站的url模式现在是这样的吗??
urlpatterns=[url](r'^admin/',admin.site.url),url(r'^',include('batches.url')),url(r'^screenings/',include('screenings.url'),]
删除其中包含
商业名称
个人
的行。你明白了吗?扎伊达齐尔,你太棒了!非常感谢你的评论!你真的帮助了我。我还有一个问题。现在我可以正确显示它们,但当我单击名称时,我没有被重定向到详细信息页面。这是我想要的吗ade错误?谢谢你的评论!谢谢zaidfazil的回答!因此你建议更改我网站的url模式,包括以下2个:url(r'^Persons/),include('Personal.url')),url(r'^BusinessName',include('BusinessName.url')),不,保留
url(r'^',include('batches.url'),
,然后删除所有重复项(即,它包含
include('batches.url')
)在主
url.py
中。然后更改模式,如我在
batches.url.py
中的回答中所示。嗨,zaidfazil,我更改了url模式。我还需要做些什么?要像这样更改两个url==>url(r'^individuals/),url(r'^businessnames/),您的网站的url模式现在看起来像这样吗???
urlpatterns=[url(r'^admin/',admin.site.url),url(r'^',include('batches.url')),url(r'^screenings/',include('screenings.url'),]
删除其中包含
业务名称
个人
的行