forms.py
categories = []
templist = []
categories_list = ArticleCategory.objects.values()
for category in categories:
templist.append(category['id'])
templist.append(str(category['name']))
categories.append(templist)
templist = []
class ArticleForm(forms.ModelForm):
name = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control py-4'}))
article = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control py-4'}))
images = forms.ImageField(widget=forms.FileInput(attrs={'class': 'form-control py-4'}))
urls = forms.CharField(widget=forms.TextInput(attrs={'class': 'form-control py-4'}))
article_categories = forms.ChoiceField(choices=categories, required=True)
class Meta:
model = Article
fields = '__all__'
widgets = {
'name': forms.TextInput(attrs={'class': 'form-control py-4'}),
'article': forms.TextInput(attrs={'class': 'form-control py-4'}),
'images': forms.FileInput(attrs={'class': 'form-control py-4'}),
'urls': forms.TextInput(attrs={'class': 'form-control py-4'}),
'article_categories': forms.Select(attrs={'class': 'table table-sm', 'size': '3'})
}
models.py
class ArticleCategory(models.Model):
name = models.CharField(max_length=128, unique=True)
description = models.TextField(null=True, blank=True)
class Meta:
verbose_name = 'Категоря'
verbose_name_plural = 'Категории'
def __str__(self):
return self.name
class Article(models.Model):
name = models.CharField(max_length=128)
article = models.TextField()
images = models.ImageField(upload_to='static/img/article_imgs')
urls = models.URLField(null=True, blank=True)
category = models.ForeignKey(to=ArticleCategory, on_delete=models.CASCADE)
class Meta:
verbose_name = 'Статья'
verbose_name_plural = 'Статьи'
def __str__(self):
return self.name
views.py
def article(request):
if request.method == 'POST':
form = ArticleForm(data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return HttpResponseRedirect(reverse('article'))
else:
form = ArticleForm()
context = {
'title': 'Статьи',
'form': form,
}
return render(request=request, template_name='user_app/article.html', context=context)
Мой HTML файл.
<form action="{% url 'article' %}" class='form-for-article' method='post' enctype="multipart/form-data">
{% csrf_token %}
<div class="col-lg-6">
<div class='form-group'>
<label for='{{ form.name.id_for_label }}'>Название</label>
{{ form.name }}
</div>
</div>
<div class="col-lg-6">
<div class='form-group'>
<label for='{{ form.article.id_for_label }}'>Описание</label>
{{ form.article }}
</div>
</div>
<div class="col-lg-6">
<div class='form-group'>
<label for="{{ form.urls.id_for.label }}">Ссылка на проект</label>
{{ form.urls }}
</div>
</div>
<div class="col-lg-6">
<div class='form-group'>
<label for='{{ form.category.id_for_label }}'>Категория</label>
{{ form.category }}
</div>
</div>
<div class="col-lg-12">
<div class="custom-file">
<label for='{{ form.images.id_for_label }}'>Фото</label>
{{ form.images }}
</div>
</div>
<div class='col-lg-12' style='margin-top: 33px;'>
<input class='btn btn-info' type="submit" value='Сохранить'>
</div>
</form>