Commit 021be428 authored by Давид Ли's avatar Давид Ли

lesson 53

parent 7ffdabc2
from webapp import models
def articles(request):
return {'articles_': models.Article.objects.all()[:10]}
......@@ -64,6 +64,7 @@ TEMPLATES = [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'core.context_processors.articles',
],
},
},
......
......@@ -26,4 +26,5 @@ urlpatterns = [
path('articles/<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('articles/<int:id>/update', views.ArticleUpdateView.as_view(), name='articles_update'),
path('articles/<int:id>/delete', views.article_delete_view, name='articles_delete'),
path('articles/<int:article_id>/comments/add', views.CommentCreateView.as_view(), name='comments_add'),
]
......@@ -27,4 +27,36 @@
<button class="btn btn-danger">Delete</button>
</form>
<hr>
<h3 class="mb-4">Comments</h3>
{% url 'comments_add' article_id=article.id as comments_action_url %}
{% include 'partial/form.html' with form=comments_form request_method='post' action_url=comments_action_url %}
{% for comment in comments %}
<div class="card my-4">
<div class="card-header">
{{ comment.author }}
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<p>{{ comment.text }}</p>
<footer class="blockquote-footer">
<cite title="Source Title">
{{ article.created_at }}
</cite>
</footer>
</blockquote>
</div>
</div>
{% empty %}
<h5>No comments yet.</h5>
{% endfor %}
{% endblock %}
\ No newline at end of file
......@@ -10,6 +10,8 @@
<h2>{{ article.title }}</h2>
<a href="{% url 'articles_detail' id=article.id %}">Подробнее...</a>
<hr>
{% empty %}
<h2>no articles yet ...</h2>
{% endfor %}
</div>
......
{% if not fields_only %}
<form method="{{ request_method|default:'get' }}">
<form method="{{ request_method|default:'get' }}" action="{{ action_url|default:'' }}">
{% csrf_token %}
{% endif %}
......
......@@ -2,7 +2,8 @@ from django.contrib import admin
from webapp import models
# admin.site.register(models.Article, ArticleAdmin)
admin.site.register(models.Comment)
@admin.register(models.Article)
class ArticleAdmin(admin.ModelAdmin):
......
......@@ -18,3 +18,9 @@ class ArticleForm(forms.ModelForm):
class SearchForm(forms.Form):
search = forms.CharField(max_length=100, required=False, label='Найти')
class CommentForm(forms.ModelForm):
class Meta:
model = models.Comment
fields = ('text', 'author')
from urllib.parse import urlencode
from django.http.response import HttpResponse as HttpResponse
from django.views import generic
from django.db.models import Q
from webapp.forms import SearchForm
from webapp.models import Article
from webapp.views.articles import (
ArticleCreateView,
ArticleDetailView,
ArticleUpdateView,
article_delete_view,
)
from webapp.views.comments import (
CommentCreateView,
)
class IndexRedirectView(generic.RedirectView):
pattern_name = 'index'
class IndexListView(generic.ListView):
template_name = 'index.html'
context_object_name = 'articles'
model = Article
ordering = ['-created_at']
paginate_by = 5
paginate_orphans = 1
def get(self, request, *args, **kwargs):
self.form = SearchForm(self.request.GET)
self.search_value = None
if self.form.is_valid():
self.search_value = self.form.cleaned_data['search']
return super().get(request, *args, **kwargs)
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(object_list=None, **kwargs)
context['form'] = self.form
if self.search_value:
context['query_params'] = urlencode({'search': self.search_value})
return context
def get_queryset(self):
qs = super().get_queryset()
if self.search_value:
query = (
Q(title__icontains=self.search_value)
| Q(author__icontains=self.search_value)
)
qs = qs.filter(query).distinct()
return qs
from typing import Any
from urllib.parse import urlencode
from django.db.models.query import QuerySet
from django.http.response import HttpResponse as HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
from django.views import generic
from django.db.models import Q
from webapp.forms import ArticleForm, SearchForm
from webapp.forms import ArticleForm, CommentForm
from webapp.models import Article
class IndexRedirectView(generic.RedirectView):
pattern_name = 'index'
class IndexView(generic.View):
def get(self, request):
context = {'articles': Article.objects.all()}
return render(request, 'index.html', context)
class IndexListView(generic.ListView):
template_name = 'index.html'
context_object_name = 'articles'
class ArticleDetailView(generic.DetailView):
pk_url_kwarg = 'id'
model = Article
ordering = ['-created_at']
paginate_by = 5
paginate_orphans = 1
def get(self, request, *args, **kwargs):
self.form = SearchForm(self.request.GET)
self.search_value = None
if self.form.is_valid():
self.search_value = self.form.cleaned_data['search']
return super().get(request, *args, **kwargs)
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(object_list=None, **kwargs)
context['form'] = self.form
if self.search_value:
context['query_params'] = urlencode({'search': self.search_value})
return context
def get_queryset(self):
qs = super().get_queryset()
if self.search_value:
query = (
Q(title__icontains=self.search_value)
| Q(author__icontains=self.search_value)
)
qs = qs.filter(query).distinct()
return qs
class ArticleDetailView(generic.TemplateView):
template_name = 'articles/detail.html'
def get_context_data(self, **kwargs):
kwargs['article'] = get_object_or_404(Article, id=kwargs['id'])
return super().get_context_data(**kwargs)
comments = self.object.comments.order_by('-created_at')
return super().get_context_data(
**kwargs,
comments=comments,
comments_form=CommentForm,
)
class ArticleCreateView(generic.FormView):
class ArticleCreateView(generic.CreateView):
model = Article
template_name = 'articles/article.html'
form_class = ArticleForm
def form_valid(self, form):
self.article = form.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('articles_detail', kwargs={'id': self.article.id})
return reverse('articles_detail', kwargs={'id': self.object.id})
class ArticleUpdateView(generic.FormView):
......@@ -112,41 +62,6 @@ class ArticleUpdateView(generic.FormView):
return reverse('articles_detail', kwargs={'id': self.article.id})
def article_update_view(request, id):
article = get_object_or_404(Article, id=id)
if request.method == 'GET':
form = ArticleForm(initial={
'title': article.title,
'text': article.text,
'author': article.author,
'tags': article.tags.all(),
})
return render(
request, 'articles/article.html',
context={'article': article, 'form': form},
)
elif request.method == 'POST':
form = ArticleForm(data=request.POST)
if form.is_valid():
tags = form.cleaned_data.pop('tags')
article.title = form.cleaned_data.get('title')
article.author = form.cleaned_data.get('author')
article.text = form.cleaned_data.get('text')
article.save()
article.tags.set(tags)
return redirect('index')
else:
return render(request, 'articles/article.html', context={'form': form, 'article': article})
def article_delete_view(request, id):
get_object_or_404(Article, id=id).delete()
......
from django.shortcuts import redirect
from django.forms.models import BaseModelForm
from django.http import HttpResponse
from django.views import generic
from webapp.models import Comment, Article
from webapp.forms import CommentForm
class CommentCreateView(generic.CreateView):
model = Comment
template_name = 'comments/comment.html'
form_class = CommentForm
def form_valid(self, form):
# article = get_object_or_404(Article, id=self.kwargs.get(''))
article_id = self.kwargs.get('article_id')
comment = form.save(commit=False)
comment.article_id = article_id
comment.save()
return redirect('articles_detail', id=article_id)
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