Issue
Models.py
class BaseLogo(models.Model):
logolabel = models.CharField(max_length = 100, null = False, blank = False)
logoimage = models.ImageField(null = False, blank = False)
def __str__(self):
return self.logolabel
Views.py
def base(request):
baselogo = get_object_or_404(BaseLogo, pk=1)
context = {
'baselogo': baselogo,
}
return render(request, 'pages/base.html', context)
Settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS= [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = '/home/flababu/VCASite/staticfiles/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Template
<a class="nav-brand" href="{% url 'main-home' %}">
<img src="{{ baselogo.logoimage.url }}" alt="VCAAT Logo" title="VCAAT Logo"
style="width: 60px; height: 60px; float: left; margin-left: 20px; margin-right: 20px;"></a>
Notes:
- It is in NavBar
- I have a gallery app and I also have profile pictures, so I was able to pass images before.
- The website is uploaded in pythonanywhere.
- I have an image uploaded in admin page
I don't understand what is wrong why my image (logo) would not appear.
Solution
I was right with my hunch that views.py was not the answer/ the problem at all. I remove my base in views.py. I created a new file custom_context_processor
custom_context_processor.py
from .models import BaseLogo
def base_renderer(request):
baselogo = BaseLogo.objects.first()
context = {
'baselogo': baselogo,
}
return context
And then added it to my
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'pages.custom_context_processor.base_renderer',
],
},
},
]
And its now running as intended, as it turned out, this is very common practice, Thank you for those who tried to help!
Answered By - Flababu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.