asdf

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