Answer the question
In order to leave comments, you need to log in
How to implement page creation in django admin?
Icons of sections will be displayed on the main page of the site: cabinets, fittings, materials, etc.
Accordingly, links will lead to pages with this content.
I want to implement the creation of such sections of the site in the admin panel.
There is a main application of the site CORE. It will have a "pages" section.
In the admin panel, you create a new one, write the title, slug (which will be the url address), description, keywords and the actual body of the page. The CORE model is given below.
I display the content of such a page in a separate template, in which the SAFE tag is specified, so that not just text is displayed, but formatted html.
Everything is fine, but tags and variables like {{ page.title }} are not displayed if they are entered in the admin panel.
What is the best way to implement the creation of pages-sections of the site through the admin panel?
After all, there should not just be html tags, but django tags to embed albums, etc.
# 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 %}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question