3

I have a project where I wish to upload an image with the Django auto generated class-based views, and it works on the admin side, but I don't know what I'm missing to make it work from an HTML page. I've searched the web with a little luck or not enough clarification.

So maybe someone can tell me what I'm missing. Here is my code:

settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'project/app/media_cdn')

models.py

from django.db import models
from django.core.urlresolvers import reverse

class Article(models.Model):
    title       = models.CharField(max_length = 200)
    ...
    thumbnail   = models.FileField(null = True, blank = True)
    ...
    def __str__(self):
        return self.title
    def get_absolute_url(self):
        return reverse('articles_detail', kwargs={'pk': self.pk})

    class Meta:
        ordering = ['-pk']

views.py

from django.shortcuts import render
from app.models import Article
from django.views.generic import *
from django.core.urlresolvers import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin

# Create your views here.
def index(request):
    return render(request, 'index.html')

class ArticleList(ListView):
    model = Article

class ArticleDetail(DetailView):
    model = Article

class ArticleCreate(LoginRequiredMixin, CreateView):
    model = Article
    fields = ['title', 'description', 'abstract', 'thumbnail', 'author', 'category', 'publishDate']

class ArticleUpdate(LoginRequiredMixin, UpdateView):
    model = Article
    fields = ['title', ..., 'thumbnail', ...]

class ArticleDelete(LoginRequiredMixin, DeleteView):
    model = Article
    success_url = reverse_lazy('articles_list')

urls.py

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from app import views, auth

urlpatterns = [
    url(r'^admin/',admin.site.urls),
    ...
    url(r'^articles/(?P<pk>[0-9]+)/$', views.ArticleDetail.as_view(),  name = 'articles_detail'),
    url(r'^articles/create/$', views.ArticleCreate.as_view(),  name = 'articles_create'),
    url(r'^articles/update/(?P<pk>[0-9]+)/$', views.ArticleUpdate.as_view(),  name = 'articles_update'),
    url(r'^articles/delete/(?P<pk>[0-9]+)/$', views.ArticleDelete.as_view(),  name = 'articles_delete'),

]

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

article_form.html

{% extends 'layout.html' %}

{% block content %}
    <form method="post" action="" enctype="multipart/form-data">
        {% csrf_token %}
        {{ form.as_p }}

        <button type="submit">Submit</button>
    </form>
{% endblock %}

As much as I was able to gather, I managed to follow all the steps but I still can't get the thumbnail to update when uploading from articles/create/ and articles/update/.../

Thanks in advance.

Prakhar Trivedi
  • 8,218
  • 3
  • 28
  • 35
Sibris
  • 41
  • 5
  • 2
    Are you sure you have `enctype="multipart/form-data"` in your form? Because in most missing-file-upload cases `enctype` is the missing part and the rest of your code looks alright to me. – narendra-choudhary Dec 22 '16 at 02:37
  • 3
    One more issue I suspect is no `upload_to` option to `thumbnail` `FileField` in `Article` model. Try adding `upload_to='.'` to `FileField`. – narendra-choudhary Dec 22 '16 at 02:46
  • as @gitblame mentioned change your FileFIeld to `thumbnail = models.FileField(null = True, blank = True, upload_to='uploads/')` . You may replace `uploads/` with any path that lies on your file system. – cutteeth Dec 22 '16 at 05:44
  • OMG! O.o ... you nailed it, THANK YOU! But... really?? upload_to='.' is really still needed with the media_root and media_url provided?!? Well, whatever, I'm just glad I got it, and wasn't too far off. – Sibris Dec 22 '16 at 05:57

0 Answers0