Home > OS >  Django .url not showing image in template
Django .url not showing image in template

Time:01-07

I was getting tired of figuring out how to fix this problem. I checked a lot of different posts and answers but nothing helped me.

I have a form where I'm creating new advert. I have file input there for Image. Form is valid and everything works fine, but my problem is that when I want to display my image from Advert model then it's not showing on website.. even the alt text.

My template code:

{% for advert in adverts %}
        <img src="{{ advert.get_image }}" alt="test"/>
          <div >
            <div >
            <div id="second-advert" >
            <div >
              <div >
                <img src="{{ advert.get_image }}" alt="test"/>
              </div>
            </div>

My urls.py:

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('carapp.urls')),
]   static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

My settings.py:

STATIC_URL = 'carsearch/static/'

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

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

My models.py:

class Advert(models.Model):

    def get_image(self):
        if self.featured_image and hasattr(self.featured_image, 'url'):
            return self.featured_image.url
        else:
            return '/path/to/default/image'


    owner = models.ForeignKey(Profile, null=True, blank=True, on_delete=models.SET_NULL)
    title = models.CharField(max_length=200)
    [...] <-- there's more code
    featured_image = models.ImageField(null=True, blank=True, default='profiles/user.png', upload_to='adverts/')
    [...] <-- there's more code



    def __str__(self):
        return self.title

My project structure:

enter image description here

And my website:

enter image description here

The image should appears above these two containers.. but it doesn't. When I go to admin panel I can see added image in my model and I can click it and see it. I also can see it in my database with python shell and with DB Browser for SQLite

But when I inspect the website then I can find my img tag but it is disabled(?)

enter image description here

enter image description here

And my views.py:

def adverts(request):
    adverts = Advert.objects.all()

    context = {'adverts': adverts, 'profile': profile}
    return render(request, 'carapp/advertsView.html', context)

I tried many things from google and nothing works.. I also can say that I have a Profile form and Profile model. And there is also a photo which I can update and everything works there..

CodePudding user response:

It looks like there might be a problem with the path to your images. In the get_image method of your Advert model, you are returning a default image if the featured_image field is not set or does not have a url attribute. However, the path to the default image is '/path/to/default/image', which does not seem to be a valid path.

You should update the path to the default image to a valid path. You can either specify the full path to the default image file, or you can use Django's static template tag to reference the image file. For example:

def get_image(self):
    if self.featured_image and hasattr(self.featured_image, 'url'):
        return self.featured_image.url
    else:
        return static('path/to/default/image')

You will also need to make sure that the default image file is included in your static files and that the STATICFILES_DIRS setting in your settings.py file includes the directory where the default image file is located.

CodePudding user response:

Actually I found a solution for this problem. Maybe it’s not what I expected but it helps.

The problem was in my featured_image field in models.py. I had to remove the attr upload_to=“images/adverts”

  • Related