python - 将 django-cms 集成到现有项目中 - 错误 : TemplateDoesNotExist at/news/cms_wizard/create/

标签 python django django-cms

我正在尝试将新闻功能添加到现有的 Django 项目中。我决定使用 django-cms 来提供功能。缺少文档,就将 django-cms 集成到现有项目而言,几乎毫无用处。

我看过this questionthis one但所提供的答案只让我走了这么远。

我想要做的是能够拥有包含静态数据的页面,并使用 django-cms 允许具有正确权限的用户创建新页面和/或更新现有页面。

目前,当我运行 python manage.py runserver 并导航到 http://127.0.0.1:8000/news/1/ 时,我得到以下错误信息:

NoReverseMatch at /news/1/ Reverse for 'pages-root' not found. 'pages-root' is not a valid view function or pattern name.

这是我的软件组件的相关版本控制信息:

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 

settings.py(相关部分)

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'

# ...

我的项目/我的项目/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('news/', include('news.urls')),
    path('admin/', admin.site.urls),
]

新闻/urls.py

from django.urls import include, path
from . import views

#app_name = 'news'


urlpatterns = [
    path('<int:id>/', views.detail, name='detail'),
    path('', include('cms.urls')),
]

新闻/模型.py

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')

新闻/views.py

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 })

新闻/模板/新闻/detail.html

{% 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

我有两个问题需要帮助解决:

  1. 当我转到 http://127.0.0.1/news/ 时显示的页面没有使用我的网站模板 - 而是使用 django 的模板-厘米。如何更改它以使用我自己的模板?

  2. 当我到达 http://127.0.0.1/news/1 时,只显示我的页面标题 - 可编辑的小部件没有移位,我无法输入文本预期的。我该如何解决这个问题?

最佳答案

  1. 要解决查找模板的问题,您需要熟悉查找模板的方法。来自documentation :

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.

由此我们可以得出结论,有必要在 INSTALLED_APPS 中将 'news' 放在 'cms' 之前:

INSTALLED_APPS = [
     ...
    'news',
     ...
     'cms',
     ...
] 
  1. 通常文本本身显示在详细信息页面上,无法更改。他们在 admin page 上更改它:

     from django.contrib import admin
     from .news.model import NewsItem
    
     class NewsItemAdmin(admin.ModelAdmin):
          pass
    
     admin.site.register(NewsItem, NewsItemAdmin)
    

如果您需要在您的网站上显示一个页面进行编辑,那么您可以使用 forms .

关于python - 将 django-cms 集成到现有项目中 - 错误 : TemplateDoesNotExist at/news/cms_wizard/create/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67452251/

相关文章:

Django-CMS 3.0.3 发布页面会重复插件 django-cms-saq 中的数据

python - pyside + py2exe,用户加载的图片不出现

python - 将 TextField() 迁移到 JSONField()

python - 为什么在将第一组信息分配给数据变量到上下文时不断收到关键错误?

python - 我应该如何在 Django 中显示 BeautifulSoup 解析的结果?

python - 无法导入名称 6 Django-CMS

python - djangocms-cascade 不在插件下拉列表中?

Python:回溯编解码器.charmap_decode(输入,self.errors,decoding_table)[0]

python - 如何在python中随机采样矩阵?

python - 通过Windows cmd在Anaconda环境中运行python脚本