sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Django: User Uploaded Images not showing in site #227

Closed brianaleew closed 1 year ago

brianaleew commented 1 year ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

Django

What's the problem you're trying to solve?

My user uploaded images refuse to show up on any page

Post any code you think might be relevant (one fenced block per file)

#this is me trying to set up media files in settings.py (project level)
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'uploads'
# project level urls.py 
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('main_app.urls')),
    path('accounts/', include('django.contrib.auth.urls')), 
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
 <!--excerpt from a template -->
 {% for apparel in apparels %}

            <div class="index-item-container  ">
              <h4>{{ apparel.name }}</h4>
              <h5> {{ apparel.brand }} </h5>
              <div><img src="{{ apparel.img }}" alt=""></div>
            </div>

          <a href="{% url 'detail' apparel.id %}">View {{ apparel.name }}</a>

          {% endfor %}

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

All other features of apparel show up except for apparel.img

What is your best guess as to the source of the problem?

I think my media files are not setup correctly. A bit confused on exactly what is needed to make user uploaded images

What things have you already tried to solve the problem?

Looked at a few articles and played around with how its accessed in the template

Paste a link to your repository here https://github.com/brianaleew/GA-Final-Project

asands94 commented 1 year ago

Are you following the way we did in class with AWS or trying out a different way?

brianaleew commented 1 year ago

No, I didnt use AWS. I just added an ImageField to the Apparel model.

asands94 commented 1 year ago

If you want to use AWS you can add a Photo model instead that has a foreign reference key of the model you want to add images to (Apparel model). I would go through the lesson we went over with AWS and let us know if there are issues. Here's a link to the Timm's CatCollector for reference: https://github.com/sei-ec-remote/cat-collector Link to the AWS Lesson: https://git.generalassemb.ly/sei-ec-remote/cat-collector/blob/main/6-django-uploading-images-s3.md