Django使用flatpages添加自定义页面

发布时间:2019-08-9 11:39--阅读:303--评论:0条

1、配置setting

INSTALLED_APPS = [
 ....
 'django.contrib.sites',
 'django.contrib.flatpages',
 ....
 ]
SITE_ID = 1
MIDDLEWARE = [
 ...
 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
 ...
]

2、配置urls.py

urlpatterns = [
path('pages/', include('django.contrib.flatpages.urls')),
]
 
或者
 
from django.contrib.flatpages import views
 
# Your other patterns here
urlpatterns += [
path('<path:url>', views.flatpage),
]
 
再或者
 
from django.contrib.flatpages import views
 
urlpatterns += [
path('about-us/', views.flatpage, {'url': '/about-us/'}, name='about'),
path('license/', views.flatpage, {'url': '/license/'}, name='license'),
]

3、添加admin

自带admin后台

from django.contrib import admin
from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from django.utils.translation import gettext_lazy as _
 
# Define a new FlatPageAdmin
class FlatPageAdmin(FlatPageAdmin):
 fieldsets = (
 (None, {'fields': ('url', 'title', 'content', 'sites')}),
 (_('Advanced options'), {
 'classes': ('collapse',),
 'fields': (
 'enable_comments',
 'registration_required',
 'template_name',
 ),
 }),
 )
 
# Re-register FlatPageAdmin
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, FlatPageAdmin)

xadmin后台

from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from django.utils.translation import gettext_lazy as _
 
class FlatPageAdmin(object):
 fieldsets = (
 (None, {'fields': ('url', 'title', 'content', 'sites')}),
 (_('Advanced options'), {
 'classes': ('collapse',),
 'fields': ( 'enable_comments',
 'registration_required',
 'template_name',
 ),
 }),
 )
 
xadmin.site.register(FlatPage, FlatPageAdmin)

数据库迁移

python manage.py makemigrations
python manage.py migrate
python manage.py runserver 8000

4、测试成功

Django使用flatpages添加自定义页面

 

这里注意一点,setting里配置SITE_ID = 1

Django使用flatpages添加自定义页面

 

Django使用flatpages添加自定义页面

 

要一致

5、添加模板

添加页面flatpages/default.html

<!DOCTYPE html>
<html>
<head>
<title>{{ flatpage.title }}</title>
</head>
<body>
{{ flatpage.content }}
</body>
</html>
或者
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endfor %}
</ul>

其他

{% get_flatpages '/about/' as about_pages %}
{% get_flatpages about_prefix as about_pages %}
{% get_flatpages '/about/' for someuser as about_pages %}

Django使用flatpages添加自定义页面

 

配置访问成功。

6、自定义内容添加富文本编辑器

前提是django配置好ckeditor编辑,参考:Django添加ckeditor富文本编辑器

修改Lib/site-packages/django/contrib/flatpages/models.py下的

class FlatPage(models.Model):
 content = models.TextField(_('content'), blank=True)

修改成:

from ckeditor_uploader.fields import RichTextUploadingField
 
 
class FlatPage(models.Model):
 content = RichTextUploadingField(_('content'), blank=True)

Django使用flatpages添加自定义页面

 

参考文章https://docs.djangoproject.com/zh-hans/2.1/ref/contrib/flatpages/

微信公众号ID:feiutech

评论列表

共0条评论

我要评论

用户名:
邮箱: