简体   繁体   中英

I am having trouble in passing a specific image in my template from Django models

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

def base(request):

baselogo = get_object_or_404(BaseLogo, pk=1)
context = {
  'baselogo': baselogo,
 }

return render(request, 'pages/base.html', context)

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/'

<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>

  1. It is in NavBar
  2. I have a gallery app and I also have profile pictures, so I was able to pass images before.
  3. The website is uploaded in pythonanywhere.
  4. I have an image uploaded in admin page

I don't understand what is wrong why my image (logo) would not appear.

in your settings.py add this 2 lines

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

media root is for telling your project where to put media files, and media url is for telling django to use this folder (link) for links (urls) so you can use it on your project and show images, if you don't tell django the media url, you are unable to show images from media files

Update:

then try this one, this code is for my project, it's very similar to what you want

in models.py in your model class

profileimage = ImageField(default="default.jpg", upload_to=your_desire_folder_path, blank=True)

you can give it any name you want, it is not related to media folder

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM