I’m working on a Django project where I’m trying to handle file uploads, but I’m encountering some issues. The uploaded files are missing, and I don’t have the full path to the media files. Here’s what I’ve done so far:
Models
I have a model with a FileField for uploads:
class Article(TrackingModel):
titleArt = models.CharField(max_length=200)
descriptionArt = models.TextField()
content = models.FileField(upload_to='articles/%Y/%m/%d')
section = models.ForeignKey(Section, on_delete=models.CASCADE)
is_accepted = models.BooleanField(default=False)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
def __str__(self):
return self.titleArt
Settings
I’m using the following settings for media files:
MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = 'media/'
URLs
I’ve set up the URL patterns like this:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('ciems_administration/', include('administration.urls')),
path('auth/', include('accounts.urls')),
path('', include('intelsight.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Template
Here’s a snippet of the template where I show the file details:
<div class="container">
<h1>Report Details</h1>
{{ article.titleArt }}<br>
{{ article.descriptionArt }}<br>
{{ article.content.url }}<br>
{{ article.section }}<br>
{{ article.created_at }}<br>
{{ article.updated_at }}<br>
<a href="{% url 'articleList' %}" class="btn btn-primary">Back</a>
</div>[](https://i.sstatic.net/O9jkZnK1.jpg)
Problem
The uploaded file paths are shown as /media/…, but the files are not actually present in the media directory.
The file upload doesn’t seem to work, and I’m not sure if the file is being saved correctly.
What I’ve Tried
Verified that MEDIA_ROOT and MEDIA_URL are correctly set.
Ensured the media directory exists and has the correct permissions.
Checked that DEBUG is True for serving media files during development.