.

parent a2c7d15c
from django import forms from django import forms
from article.models import Genre
class GenreForm(forms.Form): class GenreForm(forms.Form):
title = forms.CharField( title = forms.CharField(
required=True, required=True,
max_length=50, max_length=50,
min_length=3, min_length=3,
label="Название жанра") label="Название жанра",
description = forms.CharField(required=False, max_length=500, label="Описание", widget=forms.Textarea(attrs={'style': 'border: 2px solid red'})) widget=forms.TextInput(attrs={
"class": 'form-control',
"placeholder": "Название"
})
)
description = forms.CharField(
required=False,
max_length=500,
label="Описание",
widget=forms.Textarea(attrs={
"class": 'form-control',
"rows": '3'
}),
)
class Meta: class Meta:
fields = ['title', 'description'] fields = ['title', 'description']
......
# Generated by Django 3.2.4 on 2021-06-28 15:08 # Generated by Django 3.2.4 on 2021-06-30 14:34
from django.db import migrations, models from django.db import migrations, models
...@@ -13,6 +13,6 @@ class Migration(migrations.Migration): ...@@ -13,6 +13,6 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name='genre', model_name='genre',
name='description', name='description',
field=models.CharField(default='описание', max_length=100, verbose_name='Описание'), field=models.CharField(default='Название не указано', max_length=100, verbose_name='Описание'),
), ),
] ]
# Generated by Django 3.2.4 on 2021-06-30 08:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('article', '0005_genre_description'),
]
operations = [
migrations.AlterField(
model_name='genre',
name='description',
field=models.CharField(max_length=100, verbose_name='Описание'),
),
]
...@@ -10,8 +10,8 @@ class Author(models.Model): ...@@ -10,8 +10,8 @@ class Author(models.Model):
class Genre(models.Model): class Genre(models.Model):
title = models.CharField(max_length=50, null=False, blank=False, verbose_name='Название', ) title = models.CharField(max_length=50, null=False, blank=False, verbose_name='Название')
description = models.CharField(max_length=100, null=False, blank=False, verbose_name='Описание') description = models.CharField(max_length=100, null=False, blank=False, verbose_name='Описание', default="Название не указано")
def __str__(self): def __str__(self):
return f"{self.pk}. {self.title}" return f"{self.pk}. {self.title}"
......
...@@ -9,17 +9,28 @@ def genre_create_view(request): ...@@ -9,17 +9,28 @@ def genre_create_view(request):
return render(request, 'genre/create.html', context={'form': form}) return render(request, 'genre/create.html', context={'form': form})
if request.method == 'POST': if request.method == 'POST':
form = GenreForm(request.POST) form = GenreForm(request.POST)
if form.is_valid(): if form.is_valid():
genre = Genre.objects.create( genre = Genre.objects.create(
title=form.cleaned_data['title'], title=form.cleaned_data.get('title'),
description=form.cleaned_data['description'] description=form.cleaned_data.get('description')
) )
genre.save() genre.save()
return redirect('list_genre') return redirect('list_genre')
else:
return render(request, 'genre/create.html', context={'form': form})
return render(request, 'genre/create.html', context={'form': form}) # if request.method == 'POST':
# form = GenreForm(request.POST)
#
# if form.is_valid():
# genre = Genre.objects.create(
# title=form.cleaned_data['title'],
# description=form.cleaned_data['description']
# )
# genre.save()
# return redirect('list_genre')
#
# return render(request, 'genre/create.html', context={'form': form})
def genre_list_view(request): def genre_list_view(request):
......
{% extends 'base.html' %} {% extends 'base.html' %}
{% block content %} {% block content %}
{{ form.error.non_field_errors }}
<form action="{% url 'add_genre' %}" method="POST"> <form action="{% url 'add_genre' %}" method="POST">
{% csrf_token %} {% csrf_token %}
{{ form.non_field_errors }} {{ form.errors.title }}
<div class="form-group"> <label class="form-label" for="{{ form.title.id_for_label }}">{{ form.title.label }}</label>
{{ form.messages.title }} {{ form.title }}
<label for="{{ form.title.id_for_label }}">{{ form.title.label }}</label> {{ form.messages.description }}
{{ form.title }} <label class="form-label" for="{{ form.description.id_for_label }}">{{ form.description.label }}</label>
</div> {{ form.description }}
<div class="form-group">
{{ form.messages.description }}
<label for="{{ form.description.id_for_label }}">{{ form.description.label }}</label>
{{ form.description }}
</div>
<input type="submit" value="Создать"> <input type="submit" value="Создать">
</form> </form>
{% endblock %} {% endblock %}
\ No newline at end of file
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