.

parent a2c7d15c
from django import forms
from article.models import Genre
class GenreForm(forms.Form):
title = forms.CharField(
required=True,
max_length=50,
min_length=3,
label="Название жанра")
description = forms.CharField(required=False, max_length=500, label="Описание", widget=forms.Textarea(attrs={'style': 'border: 2px solid red'}))
label="Название жанра",
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:
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
......@@ -13,6 +13,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='genre',
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):
class Genre(models.Model):
title = models.CharField(max_length=50, null=False, blank=False, verbose_name='Название', )
description = models.CharField(max_length=100, 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='Описание', default="Название не указано")
def __str__(self):
return f"{self.pk}. {self.title}"
......
......@@ -9,17 +9,28 @@ def genre_create_view(request):
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']
title=form.cleaned_data.get('title'),
description=form.cleaned_data.get('description')
)
genre.save()
return redirect('list_genre')
else:
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):
......
{% extends 'base.html' %}
{% block content %}
{{ form.error.non_field_errors }}
<form action="{% url 'add_genre' %}" method="POST">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="form-group">
{{ form.messages.title }}
<label for="{{ form.title.id_for_label }}">{{ form.title.label }}</label>
{{ form.errors.title }}
<label class="form-label" for="{{ form.title.id_for_label }}">{{ form.title.label }}</label>
{{ form.title }}
</div>
<div class="form-group">
{{ form.messages.description }}
<label for="{{ form.description.id_for_label }}">{{ form.description.label }}</label>
<label class="form-label" for="{{ form.description.id_for_label }}">{{ form.description.label }}</label>
{{ form.description }}
</div>
<input type="submit" value="Создать">
</form>
{% 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