Lesson 50

parent e3d5c74a
from django.forms.models import ModelForm
from articles.models import Article, Author
from articles.models import Article, Author, Comment
class AuthorForm(ModelForm):
class Meta:
model = Author
fields = ("name",)
class CommentForm(ModelForm):
class Meta:
model = Comment
fields = ("text", "author")
# Generated by Django 3.2.6 on 2021-08-12 14:18
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('articles', '0003_comment'),
]
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=31, verbose_name='Тег')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Время создания')),
],
),
migrations.AlterField(
model_name='author',
name='name',
field=models.CharField(max_length=50, verbose_name='Автор'),
),
migrations.CreateModel(
name='ArticleTag',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='article_tags', to='articles.article', verbose_name='Статья')),
('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='tag_articles', to='articles.tag', verbose_name='Тег')),
],
),
migrations.AddField(
model_name='article',
name='tags',
field=models.ManyToManyField(blank=True, related_name='tags', through='articles.ArticleTag', to='articles.Tag'),
),
]
# Generated by Django 3.2.6 on 2021-08-12 14:53
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('articles', '0004_auto_20210812_1418'),
]
operations = [
migrations.AlterField(
model_name='article',
name='tags',
field=models.ManyToManyField(blank=True, related_name='articles', through='articles.ArticleTag', to='articles.Tag'),
),
]
# Generated by Django 3.2.6 on 2021-08-12 15:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('articles', '0005_alter_article_tags'),
]
operations = [
migrations.RemoveField(
model_name='article',
name='tags',
),
migrations.AddField(
model_name='article',
name='tags_old',
field=models.ManyToManyField(blank=True, related_name='articles', to='articles.Tag'),
),
]
# Generated by Django 3.2.6 on 2021-08-12 15:11
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('articles', '0006_auto_20210812_1509'),
]
operations = [
migrations.AddField(
model_name='article',
name='tags',
field=models.ManyToManyField(blank=True, related_name='articles', to='articles.Tag'),
),
migrations.AlterField(
model_name='article',
name='tags_old',
field=models.ManyToManyField(blank=True, related_name='articles_old', to='articles.Tag'),
),
]
# Generated by Django 3.2.6 on 2021-08-12 15:20
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('articles', '0007_auto_20210812_1511'),
]
operations = [
migrations.RemoveField(
model_name='article',
name='tags_old',
),
]
# Generated by Django 3.2.6 on 2021-08-12 15:20
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('articles', '0008_remove_article_tags_old'),
]
operations = [
migrations.AddField(
model_name='article',
name='tags_old',
field=models.ManyToManyField(blank=True, related_name='articles_old', through='articles.ArticleTag', to='articles.Tag'),
),
]
# Generated by Django 3.2.6 on 2021-08-12 15:20
from django.db import migrations
def transfer_tags(apps, schema_editor):
Article = apps.get_model('articles.Article')
for article in Article.objects.all():
article.tags.set(article.tags_old.all())
def rollback_transfer(apps, schema_editor):
Article = apps.get_model('articles.Article')
for article in Article.objects.all():
article.tags_old.set(article.tags.all())
class Migration(migrations.Migration):
dependencies = [
('articles', '0009_article_tags_old'),
]
operations = [
migrations.RunPython(transfer_tags, rollback_transfer)
]
# Generated by Django 3.2.6 on 2021-08-12 15:22
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('articles', '0010_auto_20210812_1520'),
]
operations = [
migrations.RemoveField(
model_name='article',
name='tags_old',
),
migrations.DeleteModel(
name='ArticleTag',
),
]
......@@ -4,6 +4,9 @@ from django.db import models
class Author(models.Model):
name = models.CharField(max_length=50, null=False, verbose_name="Автор")
def __str__(self):
return self.name
class Article(models.Model):
title = models.CharField(max_length=250, null=False)
......@@ -12,6 +15,18 @@ class Article(models.Model):
"articles.Author",
on_delete=models.SET_NULL,
null=True, related_name='articles')
tags_old = models.ManyToManyField(
'articles.Tag',
related_name="articles_old",
through='articles.ArticleTag',
through_fields=('article', 'tag'),
blank=True
)
tags = models.ManyToManyField(
'articles.Tag',
related_name="articles",
blank=True
)
class Comment(models.Model):
......@@ -27,3 +42,30 @@ class Comment(models.Model):
def __str__(self):
return self.text[:20]
class Tag(models.Model):
name = models.CharField(max_length=31, verbose_name='Тег')
created_at = models.DateTimeField(
auto_now_add=True,
verbose_name='Время создания'
)
def __str__(self):
return self.name
class ArticleTag(models.Model):
article = models.ForeignKey(
'articles.Article',
related_name='article_tags',
on_delete=models.CASCADE,
verbose_name="Статья")
tag = models.ForeignKey(
'articles.Tag',
related_name='tag_articles',
on_delete=models.CASCADE,
verbose_name="Тег")
def __str__(self):
return f"{self.article} | {self.tag}"
......@@ -14,7 +14,7 @@
{% for article in articles %}
<tr>
<th scope="row">1</th>
<td>{{ article.title }}</td>
<td><a href="{% url 'article_detail' article.pk %}">{{ article.title }}</a></td>
<td>{{ article.author.name }}</td>
</tr>
{% endfor %}
......
{% extends "base.html" %}
{% block content %}
<div class="container">
<div>
<h3>
{{ article.title }}
</h3>
<p> {{ article.body }} </p>
<p> {{ article.author }} </p>
</div>
<div>
<ul>
{% for comment in article.comments.all %}
<li>Автор: {{comment.author}} </li>
<li>Текст: {{comment.text}} </li>
<li>Дата создания: {{comment.created_at}} </li>
{% endfor %}
</ul>
</div>
<div>
<form action="{% url 'add_comment' article.pk %}" method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit"
class="btn btn-success"
value="Оставить комментарий">
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
from django.urls import path
from .views import (
article_list_view,
create_author_view,
ArticleDetailView, CommentView,
author_list_view, author_edit_view, author_delete_view, AuthorView
)
......@@ -11,6 +11,17 @@ urlpatterns = [
article_list_view,
name="article_list"
),
path(
'article/<int:pk>/',
ArticleDetailView.as_view(),
name="article_detail"
),
path(
'article/<int:article_pk>/comment/add/',
CommentView.as_view(),
name="add_comment"
),
path(
'create/',
AuthorView.as_view(),
......
......@@ -2,7 +2,7 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.views import View
from articles.models import Article, Author
from .forms import AuthorForm
from .forms import AuthorForm, CommentForm
def article_list_view(request):
......@@ -43,6 +43,29 @@ class AuthorView(View):
return redirect('author_list')
class ArticleDetailView(View):
def get(self, request, *args, **kwargs):
form = CommentForm()
return render(request, "articles/detail.html", context={
'article': get_object_or_404(Article, pk=kwargs.get("pk")),
'form': form
})
class CommentView(View):
def post(self, request, *args, **kwargs):
form = CommentForm(request.POST)
article_pk = kwargs.get('article_pk')
if form.is_valid():
article = get_object_or_404(Article, pk=article_pk)
article.comments.create(
text=request.POST.get("text"),
author=request.POST.get("author")
)
return redirect('article_detail', article_pk)
def create_author_view(request):
if request.method == "GET":
form = AuthorForm()
......
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