Dimitris Kougioumtzis Blog About technology

Add multiple content to static page

Category Django

Posted on Feb. 6, 2018



Add multiple content to static page


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import datetime
from django.db import models


class Timestamped(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class UUSlugged(models.Model):
    slug = models.SlugField()

    class Meta:
        abstract = True


class Published(models.Model):
    is_published = models.BooleanField(default=False)

    class Meta:
        abstract = True


class Page(Timestamped, Published, UUSlugged):
    name = models.CharField(max_length=100)

    class Meta:
        default_related_name = 'pages'
        verbose_name = 'page'
        verbose_name_plural = 'pages'

    def __str__(self):
        return self.name


class Content(Timestamped, Published):
    page = models.ForeignKey(Page)
    name = models.CharField(max_length=100)
    content = models.TextField(blank=True)

    class Meta:
        default_related_name = 'content'
        verbose_name = 'content'
        verbose_name_plural = 'content'

    def __str__(self):
        return self.name


def get_upload_path(instance, filename):
    return os.path.join('{}/{}/{}/{}'.format(
        datetime.datetime.now().day,
        datetime.datetime.now().month,
        datetime.datetime.now().year, filename))


class ContentImage(Timestamped, Published):
    content = models.ForeignKey(Content)
    image = models.ImageField(
        upload_to=get_upload_path, null=True, blank=True)

    class Meta:
        default_related_name = 'contentimages'
        verbose_name = 'content image'
        verbose_name_plural = 'content images'

About

My name is Dimitris Kougioumtzis and i work as a Web developer at Rapidbounce Company

Elsewhere