Display Thing database in a different webpage


#1

Hello.

I wanted to display everything in the Thing database in a different webpage other than index.html.

I tried to add the following to the views.py to get it to display:

def clerks(request):
    auctionitems = AuctionItem.objects.all()
    return render(request,'clerks.html', {'auctionitems': auctionitems,})

note: Thing is equivalent to AuctionItem

However, I cannot get any data to display in a different webpage. It only shows in index.


#2

What does your urls.py look like? Do you have a URL pointing to this view? :)


#3
from django.conf.urls import url, patterns, include
from django.contrib import admin
from collection import views
from django.views.generic import TemplateView

urlpatterns = [
    url(r'^$', views.index, name='home'),
    url(r'^clerks/$', TemplateView.as_view(template_name='clerks.html'), name='clerks'),
    url(r'^cashier/$', TemplateView.as_view(template_name='cashier.html'), name='cashier'),
    url(r'^admin/', admin.site.urls),
]

That’s what my urls.py looks like. So, I do have a URL pointed to clerks. So I don’t think that’s my problem.


#4

Here’s the issue:

url(r'^clerks/$', TemplateView.as_view(template_name='clerks.html'), name='clerks'),

This URL line is skipping the view entirely and is going straight to the template. So you can’t do any additional logic — TemplateView just shows a static website.

The line would need to be:

url(r'^clerks/$', views.clerks, name='clerks'),

In order for the logic in views.py to run. :)


#5

That totally fixed it and now it works. Thank you so much!

#6

Haha no problem, happy to help!