Add the static pages module

This commit is contained in:
Rodolphe Breard 2018-01-13 15:45:54 +01:00
parent ace88af9f9
commit e5fcd61105
12 changed files with 129 additions and 2 deletions

View file

@ -10,6 +10,7 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""
from django.utils.translation import gettext_lazy as _
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
@ -37,11 +38,14 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'khaganat',
'pages.apps.PagesConfig',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
@ -103,7 +107,14 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/
LANGUAGE_CODE = os.getenv('KHAGANAT_LANGUAGE_CODE', default='fr-fr')
LANGUAGE_CODE = os.getenv('KHAGANAT_LANGUAGE_CODE', default='fr')
# https://github.com/django/django/blob/master/django/conf/global_settings.py
LANGUAGES = [
('en', _('English')),
('fr', _('French')),
]
TIME_ZONE = os.getenv('KHAGANAT_TIME_ZONE', default='Europe/Paris')

View file

@ -13,9 +13,16 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.views.generic.base import RedirectView
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.urls import path
from django.urls import include,path
urlpatterns = [
path('', RedirectView.as_view(url='/page', permanent=False)),
path('admin/', admin.site.urls),
]
urlpatterns += i18n_patterns(
path('page/', include('pages.urls')),
)

0
pages/__init__.py Normal file
View file

11
pages/admin.py Normal file
View file

@ -0,0 +1,11 @@
from django.contrib import admin
from .models import Page,PageContent
class PageContentInline(admin.StackedInline):
model = PageContent
extra = 2
class PageAdmin(admin.ModelAdmin):
inlines = [PageContentInline]
admin.site.register(Page, PageAdmin)

5
pages/apps.py Normal file
View file

@ -0,0 +1,5 @@
from django.apps import AppConfig
class PagesConfig(AppConfig):
name = 'pages'

View file

@ -0,0 +1,35 @@
# Generated by Django 2.0.1 on 2018-01-13 14:07
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Page',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.CharField(max_length=200, unique=True)),
('default', models.BooleanField(default=False)),
],
),
migrations.CreateModel(
name='PageContent',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('language', models.CharField(choices=[('en', 'English'), ('fr', 'French')], max_length=10)),
('created_on', models.DateTimeField(auto_now_add=True)),
('edited_on', models.DateTimeField(auto_now=True)),
('title', models.CharField(max_length=200)),
('content', models.TextField()),
('page', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pages.Page')),
],
),
]

View file

21
pages/models.py Normal file
View file

@ -0,0 +1,21 @@
from django.conf import settings
from django.db import models
class Page(models.Model):
slug = models.CharField(max_length=200, unique=True)
default = models.BooleanField(default=False)
def __str__(self):
return self.slug
class PageContent(models.Model):
page = models.ForeignKey(Page, on_delete=models.CASCADE)
language = models.CharField(max_length=10, choices=settings.LANGUAGES)
created_on = models.DateTimeField(auto_now_add=True)
edited_on = models.DateTimeField(auto_now=True)
title = models.CharField(max_length=200)
content = models.TextField()
def __str__(self):
return self.title

View file

@ -0,0 +1,8 @@
{% extends "khaganat/base.html" %}
{% block title %}{{ page.title }}{% endblock %}
{% block content %}
<h1>{{ page.title }}</h1>
<p>{{ page.content }}</p>
{% endblock %}

3
pages/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
pages/urls.py Normal file
View file

@ -0,0 +1,8 @@
from django.urls import path
from . import views
urlpatterns = [
path('<slug:slug>/', views.PageView.as_view(), name='page'),
path('', views.index, name='index'),
]

18
pages/views.py Normal file
View file

@ -0,0 +1,18 @@
from django.utils.translation import get_language
from django.http import HttpResponseRedirect
from django.views import generic
from django.urls import reverse
from .models import PageContent
def index(request):
default_page = Page.objects.filter(default=True).first()
return HttpResponseRedirect(reverse('page', args=(default_page.slug, )))
class PageView(generic.DetailView):
model = PageContent
context_object_name = 'page'
template_name = 'pages/page.html'
def get_object(self):
return PageContent.objects.filter(language=get_language(), page__slug=self.kwargs['slug']).first()