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

lesson 51

parent 244ea540
......@@ -22,8 +22,8 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('', views.IndexRedirectView.as_view(), name='redirect_to_index'),
path('articles', views.IndexView.as_view(), name='index'),
path('articles/add', views.article_create_view, name='articles_add'),
path('articles/add', views.ArticleCreateView.as_view(), name='articles_add'),
path('articles/<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('articles/<int:id>/update', views.article_update_view, name='articles_update'),
path('articles/<int:id>/update', views.ArticleUpdateView.as_view(), name='articles_update'),
path('articles/<int:id>/delete', views.article_delete_view, name='articles_delete'),
]
{% extends 'base.html' %}
{% block content %}
<h1>{{ title }}</h1>
{% include 'partial/form.html' with request_method='post' %}
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<h1>Create Article</h1>
<form action="{% url 'articles_add' %}" method="post">
{% include 'partial/article_form.html' with btn_text='Create' %}
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<h1>Update Article</h1>
<form action="{% url 'articles_update' id=article.id %}" method="post">
{% include 'partial/article_form.html' %}
</form>
{% endblock %}
\ No newline at end of file
{% if not fields_only %}
<form action="{{ action_url }}" method="{{ request_method|default:'get' }}">
<form method="{{ request_method|default:'get' }}">
{% csrf_token %}
{% endif %}
......
from django import forms
from django.forms import widgets
from django.forms import ValidationError
from webapp import models
class ArticleForm(forms.Form):
title = forms.CharField(max_length=200, required=True, label='Title')
author = forms.CharField(max_length=40, required=True, label='Author')
text = forms.CharField(
max_length=3000, required=True,
label='Text', widget=widgets.Textarea,
)
tags = forms.ModelMultipleChoiceField(
required=False, label='Tags', queryset=models.Tag.objects.all(),
widget=widgets.CheckboxSelectMultiple()
)
class ArticleForm(forms.ModelForm):
class Meta:
model = models.Article
fields = ('title', 'text', 'author', 'tags')
def clean(self):
if self.data['title'] == self.data['text']:
raise ValidationError('Text should not duplicate title')
return super().clean()
from django.db import models
from django.core.validators import MinLengthValidator
class Article(models.Model):
title = models.CharField(
max_length=200, null=False, blank=False, verbose_name='Заголовок',
max_length=200, null=False,
blank=False, verbose_name='Заголовок',
validators=[MinLengthValidator(12)]
)
text = models.TextField(
null=False, blank=False, verbose_name='Текст',
......
from typing import Any
from django.http.response import HttpResponse as HttpResponse
from django.shortcuts import render, get_object_or_404, redirect
from django.urls import reverse
from django.views import generic
from webapp.forms import ArticleForm
from webapp.models import Article
# CRUD - Create Read Update Delete
class IndexRedirectView(generic.RedirectView):
pattern_name = 'index'
......@@ -28,28 +27,48 @@ class ArticleDetailView(generic.TemplateView):
return super().get_context_data(**kwargs)
def article_create_view(request):
if request.method == 'GET':
form = ArticleForm()
return render(request, 'articles/create.html', context={'form': form})
class ArticleCreateView(generic.FormView):
template_name = 'articles/article.html'
form_class = ArticleForm
elif request.method == 'POST':
form = ArticleForm(data=request.POST)
def form_valid(self, form):
self.article = form.save()
return super().form_valid(form)
if form.is_valid():
tags = form.cleaned_data.pop('tags')
def get_success_url(self):
return reverse('articles_detail', kwargs={'id': self.article.id})
article = Article.objects.create(**form.cleaned_data)
article.tags.set(tags)
return redirect('articles_detail', id=article.id)
class ArticleUpdateView(generic.FormView):
template_name = 'articles/article.html'
form_class = ArticleForm
else:
return render(
request, 'articles/create.html',
context={'form': form},
def dispatch(self, request, *args, **kwargs):
self.article = self.get_object()
return super().dispatch(request, *args, **kwargs)
def get_object(self):
return get_object_or_404(Article, id=self.kwargs.get('id'))
def get_context_data(self, **kwargs):
return super().get_context_data(
**kwargs,
article=self.article,
btn_text='Update',
)
def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
kwargs['instance'] = self.article
return kwargs
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})
def article_update_view(request, id):
article = get_object_or_404(Article, id=id)
......@@ -63,7 +82,7 @@ def article_update_view(request, id):
})
return render(
request, 'articles/update.html',
request, 'articles/article.html',
context={'article': article, 'form': form},
)
......@@ -83,7 +102,7 @@ def article_update_view(request, id):
return redirect('index')
else:
return render(request, 'articles/update.html', context={'form': form, 'article': article})
return render(request, 'articles/article.html', context={'form': form, 'article': article})
def article_delete_view(request, 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