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

lesson 49

parent ded8da55
venv
db.sqlite3
.idea
ap_12/db.sqlite3
\ No newline at end of file
......@@ -20,9 +20,10 @@ from webapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index_view, name='index'),
path('', views.IndexRedirectView.as_view(), name='redirect_to_index'),
path('articles', views.IndexView.as_view(), name='index'),
path('articles/add', views.article_create_view, name='articles_add'),
path('articles/<int:id>', views.article_detail_view, name='articles_detail'),
path('articles/<int:id>', views.ArticleDetailView.as_view(), name='articles_detail'),
path('articles/<int:id>/update', views.article_update_view, name='articles_update'),
path('articles/<int:id>/delete', views.article_delete_view, name='articles_delete'),
]
# Generated by Django 3.2.19 on 2024-01-22 13:47
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('webapp', '0002_auto_20240108_1448'),
]
operations = [
migrations.CreateModel(
name='Comment',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField(max_length=400, verbose_name='Текст')),
('author', models.CharField(blank=True, default='Аноним', max_length=40, verbose_name='Автор')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Дата обновления')),
('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='webapp.article', verbose_name='Статья')),
],
),
migrations.DeleteModel(
name='User',
),
]
......@@ -23,6 +23,17 @@ class Article(models.Model):
return f'{self.id} {self.title}'
class Comment(models.Model):
article = models.ForeignKey(
'webapp.Article',
related_name='comments',
on_delete=models.CASCADE,
verbose_name='Статья'
)
text = models.TextField(max_length=400, verbose_name='Текст')
author = models.CharField(max_length=40, blank=True, default='Аноним', verbose_name='Автор')
created_at = models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')
updated_at = models.DateTimeField(auto_now=True, verbose_name='Дата обновления')
class User(models.Model):
name = models.CharField(max_length=200, verbose_name='Имя')
def __str__(self):
return self.text[:20]
from typing import Any
from django.shortcuts import render, get_object_or_404, redirect
from django.views import generic
from webapp.forms import ArticleForm
from webapp.models import Article
......@@ -6,16 +9,23 @@ from webapp.models import Article
# CRUD - Create Read Update Delete
def index_view(request):
class IndexRedirectView(generic.RedirectView):
pattern_name = 'index'
class IndexView(generic.View):
def get(self, request):
context = {'articles': Article.objects.all()}
return render(request, 'index.html', context)
def article_detail_view(request, id):
context = {'article': get_object_or_404(Article, id=id)}
class ArticleDetailView(generic.TemplateView):
template_name = 'articles/detail.html'
return render(request, 'articles/detail.html', context)
def get_context_data(self, **kwargs):
kwargs['article'] = get_object_or_404(Article, id=kwargs['id'])
return super().get_context_data(**kwargs)
def article_create_view(request):
......
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