Home > Blockchain >  Problem in loading the logo in different URLs in Django
Problem in loading the logo in different URLs in Django

Time:06-13

I have the same html for Tools.html and home.html but I realized the logo can be loaded in this URL:

path('',views.home),

But in this URL I do not see the logo and also the favicon:

path('Tools/', views.Tools ),

http://127.0.0.1:8000/

http://127.0.0.1:8000/Tools/

views:

def Tools(request):
    p=product.objects.filter(category__name='Tools')
    return render(request,'Tools.html',{'p':p})
def home(request):
    p=product.objects.filter(category__name='home')
    return render(request,'home.html',{'p':p})

 

urls:

urlpatterns = [
    path('Tools/', views.Tools ),
    path('',views.home),
    
]

the following codes are for favicon and logo in html file:

<link rel="icon" type="image/x-icon" href="static/logo.png">
<img    width="270px"  src="static/logo.svg">

Why is this happening?

CodePudding user response:

You should not hardcode static/logo.png, the right way is to use {% static 'logo.png' %}. Then it will always search in your static folder. You just need to start the template with {% load static %}. Assuming, that you have set in settings something like:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'

and you keep such files in your_project/static folder it should work for every level on every app.

More info in Django DOCS

  • Related