Skip to content

Commit b11dc5c

Browse files
committed
Tutorial part 17 - Django article images
1 parent 60d0e30 commit b11dc5c

6 files changed

Lines changed: 44 additions & 1 deletion

File tree

2.04 MB
Binary file not shown.

17_Django-article-images/djang_website/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@
154154

155155
PASSWORD_RESET_TIMEOUT = 14400
156156

157+
DATA_UPLOAD_MAX_MEMORY_SIZE = 5242880 # 5MB
158+
157159
TINYMCE_DEFAULT_CONFIG = {
158160
'custom_undo_redo_levels': 100,
159161
'selector': 'textarea',
@@ -172,4 +174,5 @@
172174
'width': 'auto',
173175
"height": "600px",
174176
'image_caption': True,
177+
"images_upload_url": "upload_image",
175178
}

17_Django-article-images/main/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
path("<series>/<article>", views.article, name="article"),
1212
path("<series>/<article>/update", views.article_update, name="article_update"),
1313
path("<series>/<article>/delete", views.article_delete, name="article_delete"),
14+
path("<series>/<article>/upload_image", views.upload_image, name="upload_image"),
1415
]

17_Django-article-images/main/views.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
from django.http import JsonResponse
12
from django.shortcuts import render, redirect
3+
from django.views.decorators.csrf import csrf_exempt
4+
from django.conf import settings
5+
6+
from djang_website.settings import MEDIA_URL
7+
28
from .models import Article, ArticleSeries
39
from .decorators import user_is_superuser
410
from .forms import SeriesCreateForm, ArticleCreateForm, SeriesUpdateForm, ArticleUpdateForm
511

12+
import os
13+
from uuid import uuid4
14+
615
# Create your views here.
716
def homepage(request):
817
matching_series = ArticleSeries.objects.all()
@@ -153,4 +162,34 @@ def article_delete(request, series, article):
153162
"object": matching_article,
154163
"type": "article"
155164
}
156-
)
165+
)
166+
167+
@csrf_exempt
168+
@user_is_superuser
169+
def upload_image(request, series, article):
170+
if request.method != 'POST':
171+
return JsonResponse({"Error Message": "Wrong request"})
172+
173+
matching_article = Article.objects.filter(series__slug=series, article_slug=article).first()
174+
if not matching_article:
175+
return JsonResponse({"Error Message": f"Wrong series({series}) or article ({article})"})
176+
177+
file_obj = request.FILES['file']
178+
file_name_suffix = file_obj.name.split('.')[-1]
179+
if file_name_suffix not in ['jpg', 'png', 'gif', 'jpeg']:
180+
return JsonResponse({"Error Message": f"Wrong file suffix ({file_name_suffix}), supported are .jpg, .png, .git, .pjeg"})
181+
182+
file_path = os.path.join(settings.MEDIA_ROOT, 'ArticleSeries', matching_article.slug, file_obj.name)
183+
184+
if os.path.exists(file_path):
185+
file_obj.name = str(uuid4()) + '.' + file_name_suffix
186+
file_path = os.path.join(settings.MEDIA_ROOT, 'ArticleSeries', matching_article.slug, file_obj.name)
187+
188+
with open(file_path, 'wb+') as f:
189+
for chunk in file_obj.chunks():
190+
f.write(chunk)
191+
192+
return JsonResponse({
193+
"Message": "Image upload successfully",
194+
"location": os.path.join(settings.MEDIA_URL, 'ArticleSeries', matching_article.slug, file_obj.name)
195+
})
1.54 MB
Loading
106 KB
Loading

0 commit comments

Comments
 (0)