Friday, August 28, 2020

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

Hi,
    Add a default image in models.

On Thu, Aug 27, 2020 at 8:34 PM Tony Hall <thalldakota@gmail.com> wrote:
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?!!!

--
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+unsubscribe@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

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAK5m316V_u%3D6qrOMVnQgMU1aSGr7F7RMp47wgvMPjL2FLUxmew%40mail.gmail.com.

No comments:

Post a Comment