This app allows editing the contents of the static pages:
Tow types of contents
1.Text-only contents, to edit text on critical pages like the index page
2.HTML content to edit the content of the static pages like the about, term pages
Steps
1.Create app
2.Add to the installed apps
3.Declare classes in the models file
4.Register app in the admin area
5.Migrate
6.Import tables
7.Edit pages/views.py file
8.Edit templates, HTML files
1. Create App
Type python manage.py startapp content
2. Add to the installed app
In settings.py under installed apps add ‘content.apps.ContentConfig’,
# Application definition
INSTALLED_APPS = [
...
'content.apps.ContentConfig',
...
]
3. Declare classes in the models file
We declare two classes, one for text contents and one HTML contents
Edit content/models.py file
class Content(models.Model):
name = models.CharField(max_length=200)
page = models.CharField(max_length=200, default='index')
en_text = models.TextField()
ar_text = models.TextField()
# Change Name in Admin Panel
class Meta:
verbose_name = "Text Content"
verbose_name_plural = "Text Contents"
def __str__(self):
return self.name
class PagesContent(models.Model):
page_name = models.CharField(max_length=200)
en_page_title = models.CharField(max_length=200)
ar_page_title = models.CharField(max_length=200)
en_content = models.TextField()
ar_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
4. Register app in the admin area
We need to add the contents then to disallow deleting the contents or editing the name field in the admin area
* If you need to add the contents via admin you need to comment readonly_fields = ["name"] temporarily and uncomment it when adding contents is done
Edit content/admin.py file
class TextContentAdmin(admin.ModelAdmin):
list_display = ("name", "page", "en_text", "ar_text")
list_filter = ("page",)
# 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 = ["name"]
admin.site.register(Content, TextContentAdmin)
class PagesContentAdmin(SummernoteModelAdmin):
summernote_fields = ('en_content', 'ar_content')
list_display = ("page_name", "en_page_title", "ar_page_title")
# 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"]
admin.site.register(PagesContent, PagesContentAdmin)
5. Migrate
Make sure in the terminal you are in the right folder and pipenv shell is running then type
python manage.py makemigrations content
python manage.py migrate
6. Import tables
Download the attached files, use this code to import the contents from csv files to the DB
* edit the file path to point to the location of the files on your PC
COPY content_content(name,page,dob,en_text,ar_text)
FROM 'C:\tmp\content_table.csv' DELIMITER ',' CSV HEADER;
COPY content_pagescontent(page_name,en_page_title,ar_page_title,en_content,ar_content)
FROM 'C:\tmp\pages_content_table.csv' DELIMITER ',' CSV HEADER;
More resources https://www.postgresqltutorial.com/import-csv-file-into-posgresql-table/
7. Edit pages/views.py file
We are passing the contents to the pages so we need to edit the views.py file in pages app, not the content app
Edit pages/views.py file
def index(request):
index_search_box_header = Content.objects.get(name="Index Search Box Header")
high_quality = Content.objects.get(name="Lewhdk activities are high quality")
expert_hosts = Content.objects.get(name="Expert hosts")
top_activities = Content.objects.get(name="Top activities")
high_standards = Content.objects.get(name="High standards")
context = {
'index_search_box_header': index_search_box_header,
'high_quality': high_quality,
'expert_hosts': expert_hosts,
'top_activities': top_activities,
'high_standards': high_standards,
}
return render(request, 'pages/index.html', context)
def about(request):
page_content = PagesContent.objects.get(page_name="about")
context = {'page_content': page_content}
return render(request, 'pages/about.html', context)
def terms(request):
page_content = PagesContent.objects.get(page_name="terms")
context = {'page_content': page_content}
return render(request, 'pages/terms.html', context)
def privacy(request):
page_content = PagesContent.objects.get(page_name="privacy")
context = {'page_content': page_content}
return render(request, 'pages/privacy.html', context)
def faq(request):
page_content = PagesContent.objects.get(page_name="faq")
context = {'page_content': page_content}
return render(request, 'pages/faq.html', context)
def how_to_book(request):
page_content = PagesContent.objects.get(page_name="how_to_book")
context = {'page_content': page_content}
return render(request, 'pages/how-to-book.html', context)
def responsibility(request):
page_content = PagesContent.objects.get(page_name="responsibility")
context = {'page_content': page_content}
return render(request, 'pages/responsibility.html', context)
def how_to_host(request):
page_content = PagesContent.objects.get(page_name="how_to_host")
context = {'page_content': page_content}
return render(request, 'pages/how-to-host.html', context)
def signup(request):
return render(request, 'pages/signup.html', )
def login(request):
return render(request, 'pages/login.html', )
def forgot_password(request):
return render(request, 'pages/forgot-password.html', )
8. Edit templates, HTML files
To make pages editable in the admin area, you need to edit its HTML file and add this
for static content, for example in home page
Notes:
-
pages/views.py
-
pages/urls.py
-
Create an HTML page in the folder templates/pages
-
Optional - Create a record in pages content table and pass the content to the page