asdf

parent 4cda59c6
from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage from django.core.paginator import Paginator, PageNotAnInteger, EmptyPage
from django.shortcuts import render, redirect from django.shortcuts import render, redirect, get_object_or_404
from django.views import View from django.views import View
from django.views.generic import TemplateView, ListView from django.views.generic import TemplateView, ListView
from django.views.generic.base import ContextMixin
class CustomFormView(View): class CustomFormView(View):
...@@ -63,3 +64,48 @@ class ListView(TemplateView): ...@@ -63,3 +64,48 @@ class ListView(TemplateView):
def get_queryset(self): def get_queryset(self):
return self.model.objects.all() return self.model.objects.all()
class DetailView(TemplateView):
context_object_name = 'objects'
model = None
key_kwarg = 'pk'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context[self.context_object_name] = self.get_object()
return context
def get_object(self):
pk = self.kwargs.get(self.key_kwarg)
return get_object_or_404(self.model, pk=pk)
class CreateView(View):
form_class = None
template_name = None
model = None
redirect_url = None
def get(self, request, *args, **kwargs):
form = self.form_class()
context = {'form': form}
return render(request=request, template_name=self.template_name, context=context)
def post(self, request, *args, **kwargs):
form = self.form_class(data=request.POST)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
self.object = form.save()
return redirect(self.get_redirect_url())
def form_invalid(self, form):
context = {'form': form}
return render(request=self.request, template_name=self.template_name, context=context)
def get_redirect_url(self):
return self.redirect_url
\ No newline at end of file
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
<form action="{% url 'article_create' %}" method="POST">
<div class="container"> <div class="container">
{% csrf_token %} <h1>Create Article</h1>
{{ form.as_p }}
<input type="submit" value="create"> <form action="{% url 'article_create' %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="create">
</form>
</div> </div>
</form>
{% endblock %} {% endblock %}
\ No newline at end of file
...@@ -15,18 +15,20 @@ ...@@ -15,18 +15,20 @@
<li>Автор: {{comment.author}} </li> <li>Автор: {{comment.author}} </li>
<li>Текст: {{comment.text}} </li> <li>Текст: {{comment.text}} </li>
<li>Дата создания: {{comment.created_at}} </li> <li>Дата создания: {{comment.created_at}} </li>
<hr>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
<div> <div>
<form action="{% url 'add_comment' article.pk %}" method="POST">
{% csrf_token %} <form action="{% url 'create_comment' article.pk %}" method="POST">
{{ form.as_p }} {% csrf_token %}
<input type="submit" {{ form.as_p }}
class="btn btn-success" <input type="submit"
value="Оставить комментарий"> class="btn btn-success"
</form> value="Оставить комментарий">
<a href="{% url 'article_update' article.pk %}">Изменить статью</a> </form>
<a href="{% url 'article_update' article.pk %}">Изменить статью</a>
</div> </div>
</div> </div>
......
...@@ -26,10 +26,6 @@ ...@@ -26,10 +26,6 @@
{% 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' %}
......
{% extends 'base.html' %}
{% block content %}
<h1>Create Comment</h1>
<form action="{% url 'add_comment' article.pk %} ">
{{ form.as_p }}
</form>
{% endblock %}
\ No newline at end of file
from django.urls import path from django.urls import path
from .views import ( from .views import (
ArticleListView, ArticleListView,
ArticleDetailView, CommentView, ArticleDetailView,
author_edit_view, author_delete_view, AuthorView, CreateArticleView, ArticleUpdateView, AuthorListView author_edit_view, author_delete_view, AuthorView, CreateArticleView, ArticleUpdateView, AuthorListView,
ArticleCommentCreateView
) )
urlpatterns = [ urlpatterns = [
...@@ -16,13 +17,12 @@ urlpatterns = [ ...@@ -16,13 +17,12 @@ urlpatterns = [
ArticleDetailView.as_view(), ArticleDetailView.as_view(),
name="article_detail" name="article_detail"
), ),
path('articles/<int:pk>/comment/add',
ArticleCommentCreateView.as_view(),
name='create_comment'
),
path( path(
'article/<int:article_pk>/comment/add/', 'article/create',
CommentView.as_view(),
name="add_comment"
),
path(
'article/create/',
CreateArticleView.as_view(), CreateArticleView.as_view(),
name='article_create' name='article_create'
), ),
......
from django.db.models import Q from django.db.models import Q
from django.shortcuts import render, redirect, get_object_or_404 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, reverse_lazy
from django.views import View from django.views import View
from django.views.generic import FormView from django.views.generic import FormView, DetailView
from .helpers.views import CustomFormView, ListView from .helpers.views import CustomFormView, ListView, CreateView
from articles.models import Article, Author
from articles.models import Article, Author, Comment
from .forms import AuthorForm, CommentForm, ArticleForm, SearchForm from .forms import AuthorForm, CommentForm, ArticleForm, SearchForm
...@@ -58,24 +59,27 @@ class AuthorListView(ListView): ...@@ -58,24 +59,27 @@ class AuthorListView(ListView):
paginate_by = 5 paginate_by = 5
class CreateArticleView(CustomFormView): class CreateArticleView(CreateView):
template_name = 'articles/create.html' template_name = 'articles/create.html'
form_class = ArticleForm form_class = ArticleForm
model = Article
def form_valid(self, form): def get_redirect_url(self):
data = {} return reverse('article_detail', kwargs={'pk': self.object.pk})
tags = form.cleaned_data.pop('tags')
for key, value in form.cleaned_data.items():
if value is not None:
data[key] = value
self.article = Article.objects.create(**data)
self.article.tags.set(tags)
class ArticleCommentCreateView(CreateView):
model = Comment
form_class = CommentForm
template_name = 'articles/create.html'
def form_valid(self, form):
article = get_object_or_404(Article, pk=self.kwargs.get('pk'))
form.instance.article = article
return super().form_valid(form) return super().form_valid(form)
def get_redirect_url(self): def get_redirect_url(self):
return reverse('article_list') return reverse('article_detail', kwargs={'pk': self.object.article.pk})
class ArticleUpdateView(FormView): class ArticleUpdateView(FormView):
...@@ -133,13 +137,15 @@ class AuthorView(View): ...@@ -133,13 +137,15 @@ class AuthorView(View):
return redirect('author_list') return redirect('author_list')
class ArticleDetailView(View): class ArticleDetailView(DetailView):
template_name = 'articles/detail.html'
context_object_name = 'article'
model = Article
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
form = CommentForm() form = CommentForm()
return render(request, "articles/detail.html", context={ context = {'form': form, 'article': Article.objects.get(pk=kwargs.get('pk'))}
'article': get_object_or_404(Article, pk=kwargs.get("pk")), return render(request=request, template_name=self.template_name, context=context)
'form': form
})
class CommentView(View): class CommentView(View):
......
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