asdf

parent a57a45d5
from django.template.defaultfilters import register
@register.filter
def capitalize(value):
return value.capitalize()
\ No newline at end of file
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.views import View from django.views import View
from django.views.generic import TemplateView from django.views.generic import TemplateView, ListView
class CustomFormView(View): class CustomFormView(View):
...@@ -36,9 +37,29 @@ class CustomFormView(View): ...@@ -36,9 +37,29 @@ class CustomFormView(View):
class ListView(TemplateView): class ListView(TemplateView):
model = None model = None
context_key = 'objects' context_object_name = 'objects'
paginate_by = 5
paginate_orphans = 0
ordering = []
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context[self.context_key] = self.model.objects.all() objects = self.get_queryset().order_by(*self.ordering)
return context paginator = Paginator(object_list=objects, per_page=self.paginate_by, orphans=self.paginate_orphans)
\ No newline at end of file
page = self.request.GET.get('page')
try:
objects_list = paginator.page(page)
except PageNotAnInteger:
objects_list = paginator.page(1)
except EmptyPage:
objects_list = paginator.page(paginator.num_pages)
context['page_obj'] = objects_list
context[self.context_object_name] = objects_list.object_list
return context
def get_queryset(self):
return self.model.objects.all()
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
<div class="container"> <div class="container">
{% csrf_token %} {% csrf_token %}
{{ form.as_p }} {{ form.as_p }}
<input type="submit" value="create"> <input type="submit" value="create">
</div> </div>
</form> </form>
......
...@@ -26,7 +26,10 @@ ...@@ -26,7 +26,10 @@
{% endfor %} {% endfor %}
</tbody> </tbody>
</table> </table>
This text will be HTML-escaped, and will appear in all lowercase.<br>
{% filter force_escape|upper %}
This text will be HTML-escaped, and will appear in all lowercase.
{% endfilter %}
<a href="{% url 'author_create' %}">Создать автора</a> <a href="{% url 'author_create' %}">Создать автора</a>
{% include 'partial/pagination.html' %} {% include 'partial/pagination.html' %}
......
...@@ -3,9 +3,9 @@ from django.shortcuts import render, redirect, get_object_or_404 ...@@ -3,9 +3,9 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.template.defaultfilters import urlencode from django.template.defaultfilters import urlencode
from django.urls import reverse from django.urls import reverse
from django.views import View from django.views import View
from django.views.generic import FormView, ListView from django.views.generic import FormView
from .helpers.views import CustomFormView from .helpers.views import CustomFormView, ListView
from articles.models import Article, Author from articles.models import Article, Author
from .forms import AuthorForm, CommentForm, ArticleForm, SearchForm from .forms import AuthorForm, CommentForm, ArticleForm, SearchForm
...@@ -13,7 +13,6 @@ from .forms import AuthorForm, CommentForm, ArticleForm, SearchForm ...@@ -13,7 +13,6 @@ from .forms import AuthorForm, CommentForm, ArticleForm, SearchForm
class ArticleListView(ListView): class ArticleListView(ListView):
form = SearchForm form = SearchForm
search_fields = ('title', 'name') search_fields = ('title', 'name')
template_name = 'articles/list.html' template_name = 'articles/list.html'
model = Article model = Article
context_object_name = 'articles' context_object_name = 'articles'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment