# model.py
from django.db import models
from django.core import validators
from django.contrib.auth.models import AbstractUser
from django.utils.html import format_html
import pdb # pdb.set_trace()
class User(AbstractUser):
avatar = models.ImageField(upload_to='avatars', blank=True, null=True)
def thumbnail(self):
if self.avatar:
return format_html('<img src="{0}" width="100px"/>', self.avatar.url)
return 'пусто'
thumbnail.short_description = 'Превью'
class PublishedManager(models.Manager):
def get_queryset(self):
return super(PublishedManager, self).get_queryset()\
.filter(status='published')
class CommonInfo(models.Model):
'Абстрактная модель для общих полей'
title = models.CharField(max_length=200, verbose_name='Заголовок')
description = models.CharField(max_length=160, blank=True, null=True, verbose_name='Краткое описание')
keywords = models.CharField(max_length=255, blank=True, null=True, verbose_name='Ключевые слова')
class Meta:
abstract = True
class ColorField(models.CharField):
""" Поле для хранения HTML-кода цвета."""
def __init__(self, *args, **kwargs):
kwargs.setdefault('max_length', 7)
super().__init__(*args, **kwargs)
self.validators.append(validators.RegexValidator(r'#[a-f\d]{6}'))
class Pages(CommonInfo):
STATUS_CHOICES = (
('draft', 'Черновик'),
('published', 'Опубликовано'),
)
slug = models.SlugField(max_length=200, verbose_name='url статьи')
cover = models.ImageField(upload_to='cover', max_length=200, blank=True, null=True, verbose_name='Обложка')
color = ColorField(default='#FF0000', verbose_name='Цвет обложки')
body = models.TextField(blank=True, null=True, verbose_name='Содержание страницы')
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default='draft', verbose_name='Статус')
objects = models.Manager()
published = PublishedManager()
class Meta:
verbose_name = 'Страницы'
verbose_name_plural = 'Страницы'
def thumbnail(self):
if self.cover:
return format_html('<img src="{0}" width="100px"/>', self.cover.url)
return 'пусто'
thumbnail.short_description = 'Превью оболжки'
def colortile(self):
if self.color:
return format_html('<div style="background-color: {0}; \
height: 100px; width: 100px"></div>', self.color)
return 'пусто'
colortile.short_description = 'Фон'
def __str__(self):
return self.title
class DefInfo(models.Model):
ph = models.CharField(max_length=15, blank=True, null=True, verbose_name='Телефон')
soc = models.CharField(max_length=200, verbose_name='Соцсеть')
email = models.EmailField(max_length=200, verbose_name='Почта')
class Meta:
verbose_name = 'Основную информацию'
verbose_name_plural = 'Основная информация'
def __str__(self):
return self.ph
# view.py
class PageView(generic.DetailView):
model = models.Pages
template_name = 'core/page.html'
context_object_name = 'page'
# page.html
{% extends "base.html" %}
{% load static %}
{% block title %}К-МЕБЕЛЬ|{{ page.title }}{% endblock %}
{% block content %}
<h1 class="main_title">{{ page.title }}</h1>
{{ page.body|safe }}
{% endblock %}