DJANGO ADMINISTRATION ERROR & TemplateDoesNotExist

61 views
Skip to first unread message

Lightning Bit

unread,
Aug 31, 2020, 4:56:40 PM8/31/20
to Django users
I logged into the ADMIN PORTAL and saw that there was an image for each item in the ADMIN PORTAL. 

Strangely, when returning to the website, I am unable to actually "see" the pictures - they are blank with the no file icon in the top left. This was not the case back in the testing environment.

Why would this occur only upon publishing the website? It does not seem to be registering the images from DJANGO ADMINISTRATION. Also, when I click my  cart icon after adding one of the non-image showing items, I receive an error 500 stating that the cart.html and only the cart.html TemplateDoesNotExist. It has the same settings as every other html page, what makes this single page unique to becoming an error 500? 

RANGA BHARATH JINKA

unread,
Sep 1, 2020, 12:39:42 AM9/1/20
to django...@googlegroups.com
Hi,
 check in browser console for errors

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f5e9e088-110e-4d2a-a70e-43d87bd6fdb1n%40googlegroups.com.


--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

King Niko

unread,
Sep 1, 2020, 12:43:28 AM9/1/20
to django...@googlegroups.com
Interesting, the browser console states the following: 

<!doctype html>
<html lang="en">
<head>
  <title>Server Error (500)</title>
</head>
<body>
  <h1>Server Error (500)</h1><p></p>
</body>
</html>

How could the title and header 1 be causing the Server Error (500)? 

RANGA BHARATH JINKA

unread,
Sep 1, 2020, 12:45:28 AM9/1/20
to django...@googlegroups.com
open console tab not elements tab

King Niko

unread,
Sep 1, 2020, 1:44:08 AM9/1/20
to django...@googlegroups.com
The console does not seem to provide much information. It is simply stating that there is a 500 Server Error. I tried sifting for more details but there were none. 

However, in the: 

> heroku logs --tail 

It simply mentions that none of the paths exist to the cart  and the images that derive from the DJANGO ADMINISTRATION page. Strangely, all of the other pages and pictures on the website works. It is rather confusing to see only the store section of the site encountering this bug.

Kasper Laudrup

unread,
Sep 1, 2020, 5:01:02 PM9/1/20
to django...@googlegroups.com
Hi King Niko,

On 01/09/2020 07.43, King Niko wrote:
> The console does not seem to provide much information. It is simply
> stating that there is a 500 Server Error. I tried sifting for more
> details but there were none.
>

The console in your browser is not providing any information because it
doesn't make much sense to try and debug a *Server Error* on the client
side.

Ignore the advice from RANGA BHARATH JINKA on looking in your browsers
console.


> However, in the:
>
> *> heroku logs --tail *
> *
> *
> It simply mentions that none of the paths exist to /the cart / and /the
> images /that derive from the DJANGO ADMINISTRATION page.

That sounds a lot more useful, but are you really sure you're seeing the
logs from Django and not just the web server forwarding requests to your
Django application?

You need to get the logs from your actual Django application to see the
actual cause of the error. I remember there has been other discussions
on getting those logs from Heroku, but I have no experience with Heroku
so I don't know how to achieve that, but you need access to those logs
sooner or later so start by figuring out how to do that.

Kind regards,

Kasper Laudrup

King Niko

unread,
Sep 2, 2020, 11:48:20 AM9/2/20
to django...@googlegroups.com
I have not managed to get the Django log up and running, but this may help to get guidance on resolving the issue:

I have noticed that most of my bugs reside within utils.py under the "cart = {}" :

def cookieCart(request):
    #Create empty cart for now for non-logged in user
    try:
        cart = json.loads(request.COOKIES['cart'])
    except:
        cart = {}
        print('CART:', cart)

    items = []
    order = {'get_cart_total':0, 'get_cart_items':0, 'shipping':False}
    cartItems = order['get_cart_items']

    for i in cart:
        #We use try block to prevent items in cart that may have been removed from causing error
        try:
            cartItems += cart[i]['quantity']

            product = Product.objects.get(id=i)
            total = (product.price * cart[i]['quantity'])

            order['get_cart_total'] += total
            order['get_cart_items'] += cart[i]['quantity']

            item = {
                'id':product.id,
                'product':{'id':product.id,'name':product.name, 'price':product.price,
                'imageURL':product.imageURL}, 'quantity':cart[i]['quantity'],
                'digital':product.digital,'get_total':total,
                }
            items.append(item)

            if product.digital == False:
                order['shipping'] = True
        except:
            pass

    return {'cartItems':cartItems ,'order':order, 'items':items}

def cartData(request):
            if request.user.is_authenticated:
                        customer = request.user.customer
                        order, created = Order.objects.get_or_create(customer=customer, complete=False)
                        items = order.orderitem_set.all()
                        cartItems=order.get_cart_items
            else:
                        cookieData = cookieCart(request) #This is affected by the cookieCart function
                        cartItems = cookieData['cartItems']
                        order = cookieData['order']
                        items = cookieData['items']

            return{'cartItems':cartItems, 'order':order, 'items':items}  


Maybe this code is what is causing the 500 Server Error? Whenever I look at the logging in the Heroku logs, it shows the "cart = {}" from utils.py. How can I resolve this issue? Would you need to see any other pieces of code to fix this bug?
[11:30 AM]
I also implemented this into my "tests.py" which actually allowed the website to appear and be functional rather than having 500 Server Errors all throughout the website. This, somehow, fixed this 500 Server Errors on every page but the "Cart" and "Checkout". I can still "add" items to my cart and see the number increase on each page, but when I click the "Cart" I receive a 500 Server Error - the same occurs when I type in the url for the "Checkout" page. Here is the code in my "tests.py":

from django.test import TestCase, override_settings

# Create your tests here.
class MyTest(TestCase):
    @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
    def test_something(self):
        pass

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Kasper Laudrup

unread,
Sep 2, 2020, 6:07:50 PM9/2/20
to django...@googlegroups.com
Hi King Niko,

On 02/09/2020 17.46, King Niko wrote:
> *I have not managed to get the Django log up and running, but this may
> help to get guidance on resolving the issue:*
>

You should get access to the Django log. It's pointless to waste time on
guessing what might be wrong when you could have a log file pointing you
to exactly the line of code which is the cause of your error.

Anyway, regarding your code:

>     try:
>         cart = json.loads(request.COOKIES['cart'])
>     except:

Only catch the exceptions you actually expect to be thrown and know how
to handle. Catching all potential exceptions like you're doing here will
potentially silence a lot of errors making it much harder for you to
debug any issues that might occur later on because you ignored the
original error.

>         except:
>             pass

This is even more problematic. Here you are simply ignoring any
exceptions that might be thrown again making it extremely hard to debug
since you'll just get exceptions or "weird" behavior later on.

Instead of trying to guess what might be wrong, you're lucky enough to
be using an interpreted language (Python) that can give you some very
precise error messages if you stop ignoring the errors and get access to
the logs which will show exactly what went wrong.

Kind regards,

Kasper Laudrup

King Niko

unread,
Sep 3, 2020, 7:45:52 AM9/3/20
to django...@googlegroups.com
Okay, so I fixed everything with Django Logging- including getting the pictures to show up in the store.

However, one thing still remains - recognizing the store/cart.html and store/checkout.html

To get the pictures to appear from the DJANGO ADMINISTRATION, I used the following:


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('store.urls')),
    url(r'^images/(?P<path>.)$', serve,{'document_root': settings.MEDIA_ROOT}),
    url(r'^static/(?P<path>.)$', serve,{'document_root': settings.STATIC_ROOT}),


]

I also tested this to get the cart.html and checkout.html to stop showing a TemplateDoesNotExist 500 Server Error:


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('store.urls')),
    url(r'^images/(?P<path>.)$', serve,{'document_root': settings.MEDIA_ROOT}),
    url(r'^static/(?P<path>.)$', serve,{'document_root': settings.STATIC_ROOT}),
    url(r'^cart/(?P<path>.)$', serve,{'document_root': '/store/templates/store'}),
    url(r'^checkout/(?P<path>.)$', serve,{'document_root': '/store/templates/store'}),


]

But still no success. Any suggestions?



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Kasper Laudrup

unread,
Sep 3, 2020, 8:31:20 AM9/3/20
to django...@googlegroups.com
Hi King Niko,

On 03/09/2020 13.44, King Niko wrote:
> Okay, so I fixed everything with Django Logging- including getting the
> pictures to show up in the store.
>

What's written in the Django logs when you get the 500 errors?

Kind regards,

Kasper Laudrup

King Niko

unread,
Sep 3, 2020, 9:45:43 AM9/3/20
to django...@googlegroups.com
Hi Kasper, 

I am getting the following error: 

raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: ./store/cart.html

How can the template not exist if the other templates exist? Lol, it seems impossible. 


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

King Niko

unread,
Sep 3, 2020, 11:35:50 PM9/3/20
to django...@googlegroups.com
RESOLVED. I managed to fix it. 

The problem .. Was so obvious. In views.py I did not provide the cart.html and checkout.html with the same configurations as store.html. Thus, this caused the 500 server errors. I hope that this thread can help anyone in the future that encounters a similar predicament. 

Thank you all for the assistance. 
Reply all
Reply to author
Forward
0 new messages