Cohen
Cohen

Reputation: 984

Django Views---can't see the URL correctly

Inside an app called batches, I have set different url patterns. I have now 3 urls for batches, individuals and business names. It seems that when goind to the last 2 i am seeying only the info from batches all the time. Inside my app-batches i have 3 tables/classes.

Please see my url pattern of the website:

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')),
]

This is what i have in my viewes:

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")

This is what i have in my app urls:

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"),
]

If someone could help me, i would owe you a lot. I read the documentation, but i am stuck in it.

Thank you, Cohen

Upvotes: 0

Views: 60

Answers (1)

zaidfazil
zaidfazil

Reputation: 9245

The problem is that you are defining three end points for the same urls. Django searches the pattern from top to bottom and return what matches first. So, it doesn't know which of them are you specified unless you explicitly tell it. So, it'd be better to define the prefixes for end points of the urls in an app, in the same app itself.

You may define the endpoints like this in your main urls.py,

url(r'^batches/', include('batches.urls')),

then, in your app.urls.py change them accordingly,

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"),
]

Upvotes: 2

Related Questions