This commit is contained in:
Denis Zheleztsov 2017-10-10 14:19:27 +03:00
parent 1a0150e93f
commit de60ca084a
5 changed files with 33 additions and 28 deletions

View File

@ -2,6 +2,6 @@ from django.contrib import admin
from .models import Post
admin.site.register(Post
)
admin.site.register(Post)
# Register your models here.

View File

@ -3,11 +3,14 @@
from django import forms
from .models import Post
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ('name', 'file')
class DeleteForm(forms.ModelForm):
class Meta:

View File

@ -2,13 +2,14 @@
from __future__ import unicode_literals
from django.db import models
from django.utils import timezone
#from ih import images
# from ih import images
import os
class Post(models.Model):
name = models.CharField(max_length=30, blank=True)
file = models.FileField(upload_to='images')
# thumb = models.CharField(max_length=32, null=True, blank=True)
# thumb = models.CharField(max_length=32, null=True, blank=True)
def publish(self):
self.published_date = timezone.now()
@ -19,7 +20,6 @@ class Post(models.Model):
return self.name
def thumb_name(self):
x = os.path.split(self.file.name)[-1]
return '/thumb_' + x

View File

@ -4,6 +4,7 @@ from django.conf.urls import url
from . import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^$', views.image_new, name='image_new'),
url(r'^post/(?P<pk>[0-9]+)/$', views.image_detail, name='image_detail'),

View File

@ -4,7 +4,7 @@ from django.shortcuts import render, get_object_or_404, HttpResponseRedirect
from .models import Post
from .forms import PostForm, DeleteForm
from django.shortcuts import redirect
from ih import files, images
from ih import images
from django.views.decorators.csrf import requires_csrf_token
'''def create_thumbnail(request):
@ -32,9 +32,9 @@ from django.views.decorators.csrf import requires_csrf_token
def create_thumb_from_file(filename):
orig_file = 'imagehost/media/images/' + filename
#f = str(filename)
#print(filename)
#print f
# f = str(filename)
# print(filename)
# print f
# Create thumbnail
print("DEBUG: Resizing image " + orig_file)
thumb_name = images.resize_image(orig_file, {
@ -48,7 +48,6 @@ def create_thumb_from_file(filename):
return thumb_name
def image_new(request):
if request.method == "POST":
form = PostForm(request.POST, request.FILES)
@ -56,8 +55,8 @@ def image_new(request):
# thumb_name = create_thumbnail(request)
post = form.save(commit=False)
post.save()
#print("FILE: ", post.file)
print post.orig_name
# print("FILE: ", post.file)
print(post.orig_name)
create_thumb_from_file(post.orig_name)
return redirect('imagehosting.views.image_detail', pk=post.id)
else:
@ -73,12 +72,14 @@ def image_detail(request, pk):
def all_posts(request):
all = Post.objects.order_by('-pk')
page = request.POST.get("page")
# page = request.POST.get("page")
return render(request, 'imagehosting/all.html', {'all': all})
def carousel(request):
return render(request, 'imagehosting/carousel.html', {'all': Post.objects.all()})
@requires_csrf_token
def delete_post(request, pk):
post_to_delete = get_object_or_404(Post, pk=pk)