I am trying my best to learn Django for the past few days, and I came across an unusual problem. I know this is very basic form some, so I'm asking for your help on this one.
I created two different views that will show two different outputs.
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
# Create your views here.
def computers(request):
template_name = loader.get_template('computers/computers.html')
context = {
'a':[
{
'sample1': 'WMCD0001',
'sample2': 'Desktop',
'sample3': 'Lenovo ThinkPad',
'sample4': '10.10.10.100',
'sample5': 'Active',
},
]
}
return HttpResponse(template_name.render(context, request))
def newdevice(request):
return render(request, 'computers/newdevice.html')
# return HttpResponse(template.render(request, context))
I also configured the urls file on the app which goes like this
from django.urls import path
from . import views
urlpatterns = [
# path('', views.index, name='computers'),
path('', views.computers, name='computers'),
path('computers/newdevice/', views.newdevice, name='computers/newdevice')
]
The last this I wrote was the urls in the project which is like this
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('computers/', include('computers.urls')),
path('computers/newdevice/', include('computers.urls')),
path('admin/', admin.site.urls),
]
When I try to go to the localhost:8000/computers, it shows the correct view. The problem is when I try to view the page under localhost:8000/computers/newdevice it shows the same view as the one in computers.
Any help will be highly appreciated. Thank you in advance for those who will help
CodePudding user response:
Simple thing, you don't need to include computer urls multiple times because of this multiple includes for computer urls you are getting same view. Just delete that second include.
Delete this:
path('computers/newdevice/', include('computers.urls')),
And keep only this:
path('computers/', include('computers.urls')),
And now navigate to below route:
localhost:8000/computers/
localhost:8000/computers/computers/newdevice/
OR:
Change this:
path('computers/newdevice/',views.newdevice)
To this:
path('newdevice/',views.newdevice)
And navigate to below routes:
localhost:8000/computers/newdevice/
And now you will get the correct output