Make it easier to extend the default PageView.

First, extend BasePageView and add stuff to the context that your sections
need. Second, create a URLConf entry pointing to your new class. Third,
extend the base.html nav block and change the {% url cms:page %} to its new
name.

It seems so easy in hindsight...
readwriteweb
Jaap Joris Vens 2019-12-28 03:05:30 +01:00
rodzic c9a4d4e0fd
commit dc9744c5eb
8 zmienionych plików z 26 dodań i 19 usunięć

Wyświetl plik

@ -27,9 +27,9 @@ class Page(NumberedModel):
def get_absolute_url(self):
if self.slug:
return reverse('cms:page', args=[self.slug])
return reverse('page', args=[self.slug])
else:
return reverse('cms:homepage')
return reverse('page')
class Meta:
verbose_name = _('Page')

Wyświetl plik

@ -317,7 +317,7 @@ form.cms {
color: black;
border: 1px solid #aaa;
font-size: 1rem;
diplay: inline-block;
display: inline-block;
width: 100%;
box-sizing: border-box;
margin: 0;

Wyświetl plik

@ -229,7 +229,7 @@ form.cms input, form.cms select, form.cms textarea {
color: black;
border: 1px solid #aaa;
font-size: 1rem;
diplay: inline-block;
display: inline-block;
width: 100%;
box-sizing: border-box;
margin: 0;

File diff suppressed because one or more lines are too long

Wyświetl plik

@ -26,11 +26,7 @@
{% block nav %}
<ul id="menu">
{% for p in pages %}
{% if p.slug %}
<li><a href="{% url 'cms:page' p.slug %}" {% if p.pk == object.pk %}class="current"{% endif %}>{{p.title}}</a></li>
{% else %}
<li><a href="{% url 'cms:homepage' %}" {% if p.pk == object.pk %}class="current"{% endif %}>{{p.title}}</a></li>
{% endif %}
<li><a href="{% if p.slug %}{% url 'cms:page' p.slug %}{% else %}{% url 'cms:page' %}{% endif %}" {% if p.pk == object.pk %}class="current"{% endif %}>{{p.title}}</a></li>
{% endfor %}
{% if user.is_staff %}
<li><a class="edit" href="{% url 'cms:createpage' %}">+ {% trans 'new page' %}</a></li>

Wyświetl plik

@ -5,7 +5,7 @@
{% endblock %}
{% block content %}
<form method="post" class="loginform">
<form method="post" class="cms">
{% csrf_token %}
<section>

Wyświetl plik

@ -4,12 +4,14 @@ from .views import PageView, UpdatePage, CreatePage, UpdateSection, CreateSectio
app_name = 'cms'
urlpatterns = [
path('', PageView.as_view(), {'slug': ''}, name='homepage'),
path('updatepage/', UpdatePage.as_view(), {'slug': ''}, name='updatehomepage'),
path('updatepage/<int:pk>/', UpdatePage.as_view(), name='updatepage'),
path('updatesection/<int:pk>/', UpdateSection.as_view(), name='updatesection'),
path('createpage/', CreatePage.as_view(), name='createpage'),
path('createsection/<int:pk>', CreateSection.as_view(), name='createsection'),
path('createsubsection/<int:pk>/', CreateSubSection.as_view(), name='createsubsection'),
# Feel free to copy the following into your root URL conf!
path('', PageView.as_view(), name='page'),
path('<slug:slug>/', PageView.as_view(), name='page'),
path('cms/homepage/', UpdatePage.as_view(), {'slug': ''}, name='updatehomepage'),
path('cms/page/<int:pk>/', UpdatePage.as_view(), name='updatepage'),
path('cms/section/<int:pk>/', UpdateSection.as_view(), name='updatesection'),
path('cms/newpage/', CreatePage.as_view(), name='createpage'),
path('cms/page/<int:pk>/newsection/', CreateSection.as_view(), name='createsection'),
path('cms/section/<int:pk>/newsubsection/', CreateSubSection.as_view(), name='createsubsection'),
]

Wyświetl plik

@ -28,10 +28,16 @@ class MemoryMixin(object):
request.session['previous_url'] = request.path
return super().dispatch(request, *args, **kwargs)
class PageView(MenuMixin, MemoryMixin, DetailView):
class BasePageView(MenuMixin, MemoryMixin, DetailView):
model = Page
template_name = 'cms/page.html'
def setup(self, request, *args, slug='', **kwargs):
self.request = request
self.args = args
self.kwargs = kwargs
self.kwargs['slug'] = slug
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
page = self.object
@ -42,6 +48,9 @@ class PageView(MenuMixin, MemoryMixin, DetailView):
})
return context
class PageView(BasePageView):
pass
class CreatePage(StaffRequiredMixin, MenuMixin, CreateView):
model = Page
form_class = PageForm