Python/Django, "The 'poster' attribute has no file associated with it"

125 views
Skip to first unread message

Tony Hall

unread,
Aug 27, 2020, 11:04:37 AM8/27/20
to Django users
I'm relatively new to programming and I'm having some trouble with the ImageField in my models.py. 

I keep getting the error in the attachment every time I load the local:8000.

My models.py is: 

from django.db import models

class Pilot(models.Model):
title = models.CharField(max_length=200)
description = models.TextField()
count = models.IntegerField()
writer = models.CharField(max_length=200)
year = models.IntegerField()
script = models.FileField(blank=True, null=True, upload_to="scripts")
poster = models.ImageField(blank=True, null=True, upload_to="posters")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

def __str__(self):
return self.title

@property
def imageURL(self):
try:
url = self.poster.url
except:
url = ''
print('URL:', url)
return url

The settings.py :
STATIC_URL = '/static/'

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

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

The 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('script_app.urls')),
]

if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


How do I fix this?!!!
Screen Shot 2020-08-26 at 10.56.49 PM.png

RANGA BHARATH JINKA

unread,
Aug 28, 2020, 3:34:49 AM8/28/20
to django...@googlegroups.com
Hi,
    Add a default image in models.

--
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/2aae39fa-8fe5-4f83-b139-60918022ae61n%40googlegroups.com.


--
Thanks and Regards

J. Ranga Bharath
cell: 9110334114

sapna Choudhary

unread,
Aug 31, 2020, 8:02:25 AM8/31/20
to django...@googlegroups.com
Did you create media directory in your base directory?

--
Reply all
Reply to author
Forward
0 new messages