Skip to content

Instantly share code, notes, and snippets.

@amirrpp amirrpp/
Created Oct 24, 2017

What would you like to do?
from django.db import models
class AbstractSeoContent(models.Model):
h1 = models.CharField('H1', max_length=255, blank=True)
meta_title = models.CharField(_('Title for page'), max_length=255, blank=True)
meta_description = models.CharField(max_length=255, blank=True)
meta_canonical = models.CharField(max_length=255, blank=True)
meta_keywords = models.CharField(max_length=255, blank=True)
meta_robots = models.CharField(max_length=255, blank=True)
class Meta:
abstract = True
class StaticPage(AbstractSeoContent):
name = models.CharField(_('Name'), max_length=256, unique=True)
url = models.CharField(_('URL'), max_length=256)
first_text = RichTextField(_('Main text'), blank=True)
second_text = RichTextField(_('Additional text'), blank=True)
gallery = models.ForeignKey('Gallery', on_delete=models.SET_NULL,
verbose_name=_('Gallery'), null=True, blank=True)
creation_date = models.DateField(_('Date created'), auto_now_add=True, blank=True)
last_edit_date = models.DateField(_('Date updated'), auto_now=True, blank=True)
def __str__(self):
def get_absolute_url(self):
return reverse('page', kwargs={'url': self.url})
def generate_slug(self):
return slugify(
def ensure_slug_uniqueness(self):
unique_url = self.url
static_page = StaticPage.objects.exclude(
next_num = 2
while static_page.filter(url=unique_url).exists():
unique_url = '{url}_{end}'.format(url=self.url, end=next_num)
next_num += 1
if unique_url != self.url:
self.url = unique_url
def save(self, *args, **kwargs):
if self.url:
super().save(*args, **kwargs)
self.url = self.generate_slug()
super().save(*args, **kwargs)
class Meta:
app_label = 'site'
verbose_name = _('Static Page')
verbose_name_plural = _('Static Pages')
urlpatterns += [
... other urls
url(r'^(?P<url>[-_\w]+)/$', StaticPageView.as_view(), name='page'),
from django.views.generic import TemplateView
from .models import StaticPage, GlobalSettings
class StaticPageView(TemplateView):
template_name = 'static/page.html'
def get(self, request, *args, **kwargs):
self.object = get_object_or_404(StaticPage, url=kwargs.get('url'))
self.global_settings = GlobalSettings.objects.first()
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
ctx = super().get_context_data(**kwargs)
ctx['static_page'] = self.object
ctx['seo'] = self.get_seo()
return ctx
def get_meta_tag(self, tag):
tag = tag.replace('<page_name>',
tag = tag.replace('<company>',
self.global_settings.company_name if self.global_settings else '')
tag = tag.replace('<product>', '')
return tag.replace('<category>', '')
def get_seo(self):
if self.global_settings:
title = self.get_meta_tag(self.global_settings.static_page_title)
description = self.get_meta_tag(self.global_settings.static_page_description)
keywords = self.get_meta_tag(self.global_settings.static_page_keywords)
h1 = self.get_meta_tag(self.global_settings.static_page_h1)
title = ''
description = ''
keywords = ''
h1 = ''
seo = {
'title': self.object.meta_title or title,
'h1': self.object.h1 or h1,
'meta_description': self.object.meta_description or description,
'meta_canonical': self.object.meta_canonical,
'meta_keywords': keywords,
'meta_robots': self.object.meta_robots,
return seo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.