Django Dynamic Pages with translations Django.How

Author avatar wrote on 26/05/2022

1.Create a new app

python startapp pages

2.Add this app to INSTALLED_APPS list in your main file


3.Add to main file of your project

urlpatterns = i18n_patterns(
    path('', include('pages.urls')),

Better to add this to the end of urlpatterns, in this case django will use it if could not find views in other apps

4.Add file to this app

from django.urls import path

from . import views

urlpatterns = [
    path('', views.dashboard, name='dashboard'),
    path('/',, name='page'),

You can add some static pages to this app as usually

5.Add to

from django.db import models
class PagesContent(models.Model):
    lang_choices = [
        ('en', 'English'),
        ('fr', 'Français'),
        ('es', 'Español'),
        ('dt', 'Deutsch'),
        ('it', 'Italiano'),
        ('ru', 'Русский'),
        ('ua', 'Українська'),
    page_name = models.CharField(max_length=200)
    lang = models.CharField(
    slug = models.CharField(max_length=200)
    page_title = models.CharField(max_length=200, null=True)
    page_description = models.CharField(max_length=300, null=True)
    page_keywords = models.CharField(max_length=200, null=True)
    custom_styles = models.TextField(blank=True, null=True)
    custom_scripts = models.TextField(blank=True, null=True)
    content = models.TextField()

    # Change Name in Admin Panel
    class Meta: 
        verbose_name = "Page Content"
        verbose_name_plural = "Pages Contents"

    def __str__(self):
        return self.page_name

And run migrations commands

python makemigrations pages

python migrate pages

6.Add to

from django.shortcuts import get_object_or_404, render

from django.utils import translation
from .models import PagesContent

    some static views if you have

def page(request, slug):
    path = request.path
    lang = translation.get_language_from_path(path)

    if request.session.get('lang'):
        lang = request.session['lang']
        lang = 'en'

    if slug:
        check_page = PagesContent.objects.filter(slug = slug, lang = lang).count()
        if check_page > 1:
            page = PagesContent.objects.filter(slug = slug, lang = lang).first()
        elif check_page > 0:
            page = PagesContent.objects.get(slug = slug, lang = lang)
            page = get_object_or_404(PagesContent, slug = slug, lang = 'en')
    context = {
        'page': page

    return render(request,'pages/page.html', context)

7.Create file in this project folder
add to this file this code:

import time
from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect
from django.utils import translation
from django.conf import settings

from django.shortcuts import render, get_object_or_404, redirect
from django.urls import resolve, reverse

class BrowserLang():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        if not request.session.get('lang'):
        return self.get_response(request)

    def is_supported_language(self,language_code):
        supported_languages = dict(settings.LANGUAGES).keys()
        return language_code in supported_languages

    def get_browser_language(self, request):
        path = request.path
        url_parts = resolve(path)
        path_lang = translation.get_language_from_path(path)

        coockie_lang = request.COOKIES.get('user_lang') 

        if coockie_lang != None:
            request.session['lang'] = coockie_lang
            if self.is_supported_language(path_lang):
                if hasattr(request, 'session'):
                    request.session['lang'] = path_lang                
                browser_language_code = request.META.get('HTTP_ACCEPT_LANGUAGE', None)
                if browser_language_code is not None:
                    languages = [language for language in browser_language_code.split(';') if
                                '=' not in language]
                    for language in languages:
                        language_code = language.split('-')[0]
                        if self.is_supported_language(language_code):
                            if hasattr(request, 'session'):
                                request.session['lang'] = language_code


                    if not  request.session.get('lang'): 
                        if hasattr(request, 'session'):
                            request.session['lang'] = 'en'

        settings.LOGIN_URL = reverse('login')
        settings.LOGIN_REDIRECT_URL = reverse(url_parts.view_name, kwargs=url_parts.kwargs)

        url = reverse(url_parts.view_name, kwargs=url_parts.kwargs)       
        request.path_info = url
        return request

it will check users default language from browser and saving it to session, if your project don’t have this language in project setting it will set English language by default

8.Register this middleware in file


9.Add to file

from django.contrib import admin
from .models import Content, PagesContent
from django_summernote.admin import SummernoteModelAdmin

class PagesContentAdmin(SummernoteModelAdmin):
    summernote_fields = ('content','custom_styles','custom_scripts')
    list_display = ("page_name", "lang", "slug", "page_title","page_description", "page_keywords")
    list_display_links = ('page_name',)
    search_fields = ("page_name", "lang", "slug", "page_title","page_description", "page_keywords")
    list_per_page = 20

    # Remove Delete Seleted
    def get_actions(self, request):
        actions = super().get_actions(request)
        if 'delete_selected' in actions:
            del actions['delete_selected']
        return actions

    # Disallow Delete
    def has_delete_permission(self, request, obj=None):
        return False

    # Make Name Read Only
    #readonly_fields = ["page_name"], PagesContentAdmin)

Important: This example using django_summernote package!!! You need to install it using this guide - Connect to preview

Now its done.