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

lesson 50

parent d1ebeea8
......@@ -27,6 +27,6 @@ urlpatterns = [
# /articles/1/ OR /articles/?id=1
# article_details_view(request, id)
path('articles/<int:id>/', views.ArticleDetailView.as_view(), name='articles-detail'),
path('articles/<int:id>/edit', views.article_update_view, name='article_update'),
path('articles/<int:id>/edit', views.ArticleUpdateView.as_view(), name='article_update'),
path('articles/<int:id>/delete', views.delete_article, name='delete_article')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
from django.forms import ValidationError
def at_least_5(v):
if len(v) < 5:
raise ValidationError(
'This field should be at least %(length)d symbols long',
code='too_short',
params={'length': 5}
)
asgiref==3.7.2
Django==3.2.19
django-extensions==3.2.3
pytz==2023.3
sqlparse==0.4.4
from django import forms
from django.forms import widgets
from django.forms import widgets, ValidationError
from core.validators import at_least_5
from web.models import Article
......@@ -22,3 +23,10 @@ class ArticleModelForm(forms.ModelForm):
'text': forms.Textarea(attrs={'class': 'form-control mb-3'}),
'tags': forms.CheckboxSelectMultiple(attrs={'class': 'mb-3'})
}
def clean(self):
cleaned_data = super().clean()
if cleaned_data.get('text') == cleaned_data.get('title'):
raise ValidationError('Text of the article should not duplicate it\'s title')
return cleaned_data
# Generated by Django 3.2.19 on 2023-07-13 11:12
import core.validators
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('web', '0011_auto_20230710_1818'),
]
operations = [
migrations.AlterField(
model_name='article',
name='title',
field=models.CharField(max_length=200, validators=[core.validators.at_least_5], verbose_name='Заголовок'),
),
]
from django.db import models
from django.db.models import TextChoices
from django.core.validators import MinLengthValidator
class StatusChoices(TextChoices):
......@@ -15,7 +16,10 @@ class Article(models.Model):
max_length=200,
null=False, # <null>
blank=False, # ''
verbose_name='Заголовок'
verbose_name='Заголовок',
validators=[
MinLengthValidator(5)
]
)
tags = models.ManyToManyField('web.Tag', related_name='articles', blank=True)
......
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.views.generic import View, TemplateView, RedirectView
from django.views.generic import View, TemplateView, RedirectView, FormView
from web.forms import ArticleForm, ArticleModelForm
from web.models import Article, StatusChoices
......@@ -56,6 +56,34 @@ class ArticleDetailView(TemplateView):
article = get_object_or_404(Article, id=kwargs.get('id'))
return super().get_context_data(article=article)
class ArticleUpdateView(FormView):
template_name = 'update.html'
form_class = ArticleModelForm
def dispatch(self, request, *args, **kwargs):
self.article = get_object_or_404(self.form_class.Meta.model, id=kwargs.get('id'))
return super().dispatch(request, *args, **kwargs)
def get_context_data(self, **kwargs):
return super().get_context_data(article=self.article, **kwargs)
def get_initial(self):
initial = self.article.__dict__
initial['tags'] = self.article.tags.all()
return initial
def get_form_kwargs(self):
return super().get_form_kwargs() | {'instance': self.article}
def form_valid(self, form):
form.save()
return super().form_valid(form)
def get_success_url(self):
return reverse('articles-detail', kwargs={'id': self.article.id})
def article_update_view(request, id):
article = get_object_or_404(Article, pk=id)
if request.method == 'GET':
......
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