Commit 9268aee8 authored by Otto Naderer's avatar Otto Naderer

got imageapp pic inclusion working

parent a5c62483
from blogapp.models import blogentry, blogcomment
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext
# from django.template import RequestContext
from django import forms
from imageapp.models import BlogImage
import datetime
import mistune
class CustomRenderer(mistune.Renderer):
def image(self, src, title, alt_text):
return '<div id="textimg"><img src="' + src + '"/><br>Title: ' + title + '<br>Alt: ' + alt_text + '</div>'
try:
img = BlogImage.objects.get(pk=src)
return '<div id="textimg"><img width="100%" src="' + img.image.url + '"/><br>Title: ' + img.title + '<br>Caption: ' + img.caption + '</div>'
except BlogImage.DoesNotExist:
return '<i>Should display an image here but none with id ' + src + ' was found.</i>'
class BlogCommentForm(forms.Form):
guestname = forms.CharField()
blogcomment = forms.CharField(widget=forms.Textarea(attrs={'rows':3, 'cols':30}))
blogcomment = forms.CharField(widget=forms.Textarea(attrs={'rows': 3, 'cols': 30}))
def listall(request):
listall_entries = blogentry.objects.all().order_by('-date')[:10]
context = {'blogentries': listall_entries}
return render(request, 'blogapp/blogindex.html', context)
listall_entries = blogentry.objects.all().order_by('-date')[:10]
context = {'blogentries': listall_entries}
return render(request, 'blogapp/blogindex.html', context)
def detail(request, blogentry_id):
blogdetail = get_object_or_404(blogentry, pk=blogentry_id)
if request.method == 'POST':
fml = BlogCommentForm(request.POST)
if fml.is_valid():
bc = blogcomment()
bc.blogentry = blogdetail
bc.guestname = fml.cleaned_data['guestname']
bc.body = fml.cleaned_data['blogcomment']
bc.date = datetime.datetime.now()
bc.save
renderer = CustomRenderer()
md = mistune.Markdown(renderer = renderer, hard_wrap = True)
blogdetail.body = md(blogdetail.body)
......@@ -28,58 +47,59 @@ def detail(request, blogentry_id):
form = BlogCommentForm()
context = {'blogentry': blogdetail, 'commentform': form}
return render(request, 'blogapp/blogdetail.html', context)
#if request.user.is_authenticated():
# return render_to_response('blogapp/blogdetail.html', {'blogentry': blogdetail, 'commentform': form}, context_instance=RequestContext(request))
#else:
# return render_to_response('blogapp/blogdetail.html', {'blogentry': blogdetail}, context_instance=RequestContext(request))
# if request.user.is_authenticated():
# return render_to_response('blogapp/blogdetail.html', {'blogentry': blogdetail, 'commentform': form}, context_instance=RequestContext(request))
# else:
# return render_to_response('blogapp/blogdetail.html', {'blogentry': blogdetail}, context_instance=RequestContext(request))
#def addarticle(request):
# if not request.user.is_staff:
# return listall(request)
# def addarticle(request):
# if not request.user.is_staff:
# return listall(request)
#
# if request.method == 'POST':
# form = NewsForm(request.POST)
# if form.is_valid():
# blog = blogentry()
# blog.user = request.user
# blog.newsheader = form.cleaned_data['header']
# blog.newsbody = form.cleaned_data['body']
# blog.date = datetime.datetime.now()
# blog.save()
# if request.method == 'POST':
# form = NewsForm(request.POST)
# if form.is_valid():
# blog = blogentry()
# blog.user = request.user
# blog.newsheader = form.cleaned_data['header']
# blog.newsbody = form.cleaned_data['body']
# blog.date = datetime.datetime.now()
# blog.save()
#
#
# #return rather to the thread detail here
# listall_entries = newsentry.objects.all().order_by('date')[:10]
# return render_to_response('blogindex.html', {'listall_entries': listall_entries}, context_instance=RequestContext(request))
# else:
# form = NewsForm()
# return render_to_response('blogindex.html', {'showaddnewsform' : form}, context_instance=RequestContext(request))
# #return rather to the thread detail here
# listall_entries = newsentry.objects.all().order_by('date')[:10]
# return render_to_response('blogindex.html', {'listall_entries': listall_entries}, context_instance=RequestContext(request))
# else:
# form = NewsForm()
# return render_to_response('blogindex.html', {'showaddnewsform' : form}, context_instance=RequestContext(request))
#
#def editnews(request, newsentry_id):
# if not request.user.is_staff:
# return listall(request)
# def editnews(request, newsentry_id):
# if not request.user.is_staff:
# return listall(request)
#
# newsdetail = get_object_or_404(newsentry, pk=newsentry_id)
# newsdetail = get_object_or_404(newsentry, pk=newsentry_id)
#
# if request.method == 'POST':
# form = NewsForm(request.POST)
# if form.is_valid():
# newsdetail.newsheader = form.cleaned_data['newsheader']
# newsdetail.newsbody = form.cleaned_data['newsbody']
# newsdetail.save()
# return detail(request, newsentry_id)
# if request.method == 'POST':
# form = NewsForm(request.POST)
# if form.is_valid():
# newsdetail.newsheader = form.cleaned_data['newsheader']
# newsdetail.newsbody = form.cleaned_data['newsbody']
# newsdetail.save()
# return detail(request, newsentry_id)
#
#
# data = {'newsheader': newsdetail.newsheader, 'newsbody': newsdetail.newsbody}
# form = NewsForm(data)
# return render_to_response('newsedit.html', {'editform' : form}, context_instance=RequestContext(request))
# data = {'newsheader': newsdetail.newsheader, 'newsbody': newsdetail.newsbody}
# form = NewsForm(data)
# return render_to_response('newsedit.html', {'editform' : form}, context_instance=RequestContext(request))
#
#d#ef showfrontpage(request):
#0 newsdetail = get_object_or_404(newsentry, pk=1)
# return render_to_response('newsapp/newsdetail.html', {'newsentry': newsdetail, 'isfrontpage': True}, context_instance=RequestContext(request))
# def showfrontpage(request):
# 0 newsdetail = get_object_or_404(newsentry, pk=1)
# return render_to_response('newsapp/newsdetail.html', {'newsentry': newsdetail, 'isfrontpage': True}, context_instance=RequestContext(request))
#
#
#d#ef getincludes(request):
# eventlist = evententry.objects.all().order_by('-date')[:10]
# appointmentlist = event.objects.all().filter(eventend__gte = datetime.datetime.now()).order_by('eventstart')[:5]
# return {'eventlist' : eventlist, 'appointmentlist' : appointmentlist}
# def getincludes(request):
# eventlist = evententry.objects.all().order_by('-date')[:10]
# appointmentlist = event.objects.all().filter(eventend__gte = datetime.datetime.now()).order_by('eventstart')[:5]
# return {'eventlist' : eventlist, 'appointmentlist' : appointmentlist}
No preview for this file type
from django.db import models
from uuid import uuid4
from django.utils.deconstruct import deconstructible
import os
from polysite import settings
# Create your models here.
@deconstructible
class UploadToPathAndRename(object):
def __init__(self, path):
self.sub_path = path
def __call__(self, instance, filename):
ext = filename.split('.')[-1]
# get filename
if instance.pk:
filename = '{}.{}'.format(instance.pk, ext)
else:
# set filename as random string
filename = '{}.{}'.format(uuid4().hex, ext)
# return the whole path to the file
return os.path.join(self.sub_path, filename)
class BlogImage(models.Model):
title = models.CharField(max_length = 100)
caption = models.CharField(max_length = 300)
date = models.DateTimeField()
image = models.ImageField(upload_to = 'media/')
image = models.ImageField(upload_to = UploadToPathAndRename(""))
def __str__(self):
return self.title
......
media/2.jpg

1.56 MB

......@@ -130,4 +130,4 @@ STATICFILES_DIRS = [
]
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = 'http://127.0.0.1:8000/media/'
MEDIA_URL = '/media/'
......@@ -16,8 +16,10 @@ Including another URLconf
from django.conf.urls import url, include
from django.contrib import admin
from pageapp.views import aboutpage, contactpage
from blogapp.views import listall
from . import views
#from blogapp.views import listall
#from django.conf import settings
from django.conf.urls.static import static
from . import views, settings
urlpatterns = [
url(r'^$', views.redi),
......@@ -25,5 +27,5 @@ urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^about/', aboutpage, name='about'),
url(r'^contact/', contactpage, name='contact'),
# url(r'^', listall),
]
# url(r'^', listall),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
......@@ -46,7 +46,6 @@ div#main{
div#textimg{
padding-top:1em;
display: block;
margin-left: auto;
margin-right: auto;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment