I am trying to add news functionality to an existing django project. I decided to use django-cms to provide the functionality. The documentation is lacking and what little there is, is next to useless, in terms of integrating django-cms into an existing project.
I have looked at this question and this one but the offered answers only take me so far.
What I want to do is to be able to have pages with static data, and use django-cms to allow users with the right permissions to create new pages and/or update existing pages.
At the moment, when I run python manage.py runserver
and I navigate to http://127.0.0.1:8000/news/1/
, I get the following error message:
NoReverseMatch at /news/1/ Reverse for 'pages-root' not found. 'pages-root' is not a valid view function or pattern name.
Here is the relevant versioning information for my software components:
Django 3.2.2
django-classy-tags 2.0.0
django-cms 3.8.0
django-formtools 2.3
django-sekizai 2.0.0
django-treebeard 4.5.1
djangocms-admin-style 2.0.2
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'cms',
'menus',
'treebeard',
'sekizai',
'news',
]
SITE_ID = 1
SITE_ID = 1
X_FRAME_OPTIONS = "ALLOWALL"
XS_SHARING_ALLOWED_METHODS = ["POST", "GET", "OPTIONS", "PUT", "DELETE"]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
'cms.middleware.user.CurrentUserMiddleware',
'cms.middleware.page.CurrentPageMiddleware',
'cms.middleware.toolbar.ToolbarMiddleware',
'cms.middleware.language.LanguageCookieMiddleware',
]
LANGUAGES = [
('en', 'English'),
]
CMS_TEMPLATES = (
('template_1.html', 'Template One'),
('template_2.html', 'Template Two'),
)
ROOT_URLCONF = 'blogproject.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'sekizai.context_processors.sekizai',
'cms.context_processors.cms_settings',
],
},
},
]
WSGI_APPLICATION = 'blogproject.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en'
# ...
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('news/', include('news.urls')),
path('admin/', admin.site.urls),
]
from django.urls import include, path
from . import views
#app_name = 'news'
urlpatterns = [
path('<int:id>/', views.detail, name='detail'),
path('', include('cms.urls')),
]
from django.db import models
from cms.models.fields import PlaceholderField
# Create your models here.
class NewsItem(models.Model):
# your fields
title = models.TextField()
content = PlaceholderField('placeholder_name')
from django.shortcuts import get_object_or_404, render
from .models import NewsItem
# Create your views here.
def detail(request, id):
object = get_object_or_404(NewsItem, id=id)
return render(request, 'news/detail.html', { 'object': object })
{% load cms_tags %}
{{ object.title }}
{% render_placeholder object.content language 'en' %}
news/templates/
└── news
├── cms_wizard
│ └── create
│ └── template_1.html
├── detail.html
└── template_1.html
I have two issues that I need help solving:
The page that is displayed when I go to http://127.0.0.1/news/
is not using my template for my website - rather it is using a template from django-cms. How do I change it to use my own template?
When I got to http://127.0.0.1/news/1
only my page title is shown - the editable widget is not displaced, and I can't enter text as expected. How do I fix this?
Loads templates from Django apps on the filesystem. For each app in INSTALLED_APPS, the loader looks for a templates subdirectory. If the directory exists, Django looks for templates in there.
For example, for this setting:
INSTALLED_APPS = ['myproject.polls', 'myproject.music']
… Then get_template ('foo.html') will look for foo.html in these directories, in this order:
/ path / to / myproject / polls / templates / / path / to / myproject / music / templates /
... and will use the one it finds first.
From this we can conclude that it is necessary to put 'news' in INSTALLED_APPS before 'cms':
INSTALLED_APPS = [
...
'news',
...
'cms',
...
]
Usually the text itself is displayed on the detail page, without the ability to change it. And they change it on the admin page:
from django.contrib import admin
from .news.model import NewsItem
class NewsItemAdmin(admin.ModelAdmin):
pass
admin.site.register(NewsItem, NewsItemAdmin)
If you need to display a page for editing on your site, then you can use the forms.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With