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

lessn 50

parent ece54624
venv
db.sqlite3
.idea
ap_12/db.sqlite3
\ No newline at end of file
*/db.sqlite3
\ No newline at end of file
No preview for this file type
......@@ -7,5 +7,4 @@ body {
.container {
margin: 0 auto;
width: 80%;
text-align: center;
}
......@@ -7,6 +7,17 @@
<p>{{ article.text }}</p>
<p>By: {{ article.author }}</p>
<h2>Tags</h2>
<ul class="w-25">
{% for tag in article.tags.all %}
<li>{{ tag.name }}</li>
{% endfor %}
</ul>
<a href="{% url 'articles_update' id=article.id %}" class="btn btn-primary">
Update
</a>
......
{% extends 'base.html' %}
{% block content %}
<div class="text-center">
<h1>Articles</h1>
<p><a href="{% url 'articles_add' %}">Добавить</a></p>
......@@ -10,5 +11,7 @@
<a href="{% url 'articles_detail' id=article.id %}">Подробнее...</a>
<hr>
{% endfor %}
</div>
{% endblock %}
\ No newline at end of file
from django import forms
from django.forms import widgets
from webapp import models
class ArticleForm(forms.Form):
title = forms.CharField(max_length=200, required=True, label='Title')
......@@ -9,3 +11,7 @@ class ArticleForm(forms.Form):
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()
)
# Generated by Django 3.2.19 on 2024-01-25 13:42
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('webapp', '0003_auto_20240122_1347'),
]
operations = [
migrations.CreateModel(
name='Tag',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30, verbose_name='Тег')),
],
),
migrations.CreateModel(
name='ArticleTag',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('created_at', models.DateTimeField(auto_now_add=True)),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='article_tags', to='webapp.article')),
('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tag_articles', to='webapp.tag')),
],
),
migrations.AddField(
model_name='article',
name='tags',
field=models.ManyToManyField(related_name='article', through='webapp.ArticleTag', to='webapp.Tag'),
),
]
# Generated by Django 3.2.23 on 2024-01-25 13:48
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0004_auto_20240125_1342'),
]
operations = [
migrations.AlterField(
model_name='article',
name='tags',
field=models.ManyToManyField(related_name='articles', through='webapp.ArticleTag', to='webapp.Tag'),
),
]
# Generated by Django 3.2.19 on 2024-01-25 14:02
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('webapp', '0005_alter_article_tags'),
]
operations = [
migrations.RenameField(
model_name='article',
old_name='tags',
new_name='tags_old',
),
]
# Generated by Django 3.2.19 on 2024-01-25 14:02
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0006_rename_tags_article_tags_old'),
]
operations = [
migrations.AddField(
model_name='article',
name='tags',
field=models.ManyToManyField(related_name='articles', to='webapp.Tag'),
),
migrations.AlterField(
model_name='article',
name='tags_old',
field=models.ManyToManyField(related_name='articles_set', through='webapp.ArticleTag', to='webapp.Tag'),
),
]
# Generated by Django 3.2.19 on 2024-01-25 14:04
from django.db import migrations
def transfer_tags(apps, schema_editor):
Article = apps.get_model('webapp.Article')
for article in Article.objects.all():
article.tags.set(article.tags_old.all())
def rollback_transfer(apps, schema_editor):
Article = apps.get_model('webapp.Article')
for article in Article.objects.all():
article.tags_old.set(article.tags.all())
class Migration(migrations.Migration):
dependencies = [
('webapp', '0007_auto_20240125_1402'),
]
operations = [
migrations.RunPython(transfer_tags, rollback_transfer)
]
......@@ -15,6 +15,9 @@ class Article(models.Model):
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Дата обновления')
tags_old = models.ManyToManyField('webapp.Tag', related_name='articles_set', through='webapp.ArticleTag')
tags = models.ManyToManyField('webapp.Tag', related_name='articles')
class Meta:
verbose_name = 'статья'
verbose_name_plural = 'статьи'
......@@ -37,3 +40,16 @@ class Comment(models.Model):
def __str__(self):
return self.text[:20]
class Tag(models.Model):
name = models.CharField(max_length=30, verbose_name='Тег')
def __str__(self):
return self.name
class ArticleTag(models.Model):
article = models.ForeignKey('webapp.Article', on_delete=models.CASCADE, related_name='article_tags')
tag = models.ForeignKey('webapp.Tag', on_delete=models.CASCADE, related_name='tag_articles')
created_at = models.DateTimeField(auto_now_add=True)
......@@ -34,15 +34,14 @@ def article_create_view(request):
return render(request, 'articles/create.html', context={'form': form})
elif request.method == 'POST':
print(request.POST)
form = ArticleForm(data=request.POST)
if form.is_valid():
article = Article.objects.create(
title=request.POST.get('title'),
text=request.POST.get('text'),
author=request.POST.get('author'),
)
tags = form.cleaned_data.pop('tags')
article = Article.objects.create(**form.cleaned_data)
article.tags.set(tags)
return redirect('articles_detail', id=article.id)
else:
......@@ -60,6 +59,7 @@ def article_update_view(request, id):
'title': article.title,
'text': article.text,
'author': article.author,
'tags': article.tags.all(),
})
return render(
......@@ -71,12 +71,16 @@ def article_update_view(request, id):
form = ArticleForm(data=request.POST)
if form.is_valid():
tags = form.cleaned_data.pop('tags')
article.title = form.cleaned_data.get('title')
article.author = form.cleaned_data.get('author')
article.text = form.cleaned_data.get('text')
article.save()
article.tags.set(tags)
return redirect('index')
else:
return render(request, 'articles/update.html', context={'form': form, 'article': article})
......
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