asdfasd

parent 7d4e2da2
# Generated by Django 3.2.4 on 2021-06-24 14:01
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('article', '0002_auto_20210623_1935'),
]
operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100, verbose_name='Имя')),
('last_name', models.CharField(max_length=100, verbose_name='Название')),
],
),
migrations.AlterField(
model_name='article',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='article.author', verbose_name='Автор'),
),
]
from django.db import models from django.db import models
class Author(models.Model):
name = models.CharField(max_length=100, null=False, blank=False, verbose_name='Имя')
last_name = models.CharField(max_length=100, null=False, blank=False, verbose_name='Название')
def __str__(self):
return f"{self.name} {self.last_name}"
class Article(models.Model): class Article(models.Model):
title = models.CharField(max_length=100, null=False, blank=False, verbose_name='Название') title = models.CharField(max_length=100, null=False, blank=False, verbose_name='Название')
content = models.TextField(max_length=3000, null=False, blank=False, verbose_name='Тело') content = models.TextField(max_length=3000, null=False, blank=False, verbose_name='Тело')
author = models.CharField(max_length=100, null=False, blank=False, verbose_name='Автор') author = models.ForeignKey(to=Author, on_delete=models.CASCADE, verbose_name='Автор')
created_at = models.DateTimeField(auto_now_add=True, verbose_name="Дата и время создания") created_at = models.DateTimeField(auto_now_add=True, verbose_name="Дата и время создания")
updated_at = models.DateTimeField(auto_now=True, verbose_name="Дата и время изменения") updated_at = models.DateTimeField(auto_now=True, verbose_name="Дата и время изменения")
publication_date = models.DateTimeField(verbose_name="Дата и время публикации", null=True, blank=False) publication_date = models.DateTimeField(verbose_name="Дата и время публикации", null=True, blank=False)
...@@ -13,6 +20,5 @@ class Article(models.Model): ...@@ -13,6 +20,5 @@ class Article(models.Model):
return f"{self.pk}. {self.title}" return f"{self.pk}. {self.title}"
class Meta: class Meta:
verbose_name='Статья' verbose_name = 'Статья'
verbose_name_plural='Статьи' verbose_name_plural = 'Статьи'
from django.urls import path from django.urls import path
from article.views import article_create_view, article_list_view, article_detail_view, article_update_view from article.views import \
article_create_view, \
article_list_view, \
article_detail_view, \
article_update_view, \
author_list_view, \
author_create_view
urlpatterns = [ urlpatterns = [
...@@ -7,4 +13,7 @@ urlpatterns = [ ...@@ -7,4 +13,7 @@ urlpatterns = [
path('list/', article_list_view, name='article_list'), path('list/', article_list_view, name='article_list'),
path('detail/<int:pk>', article_detail_view, name='article_detail'), path('detail/<int:pk>', article_detail_view, name='article_detail'),
path('edit/<int:pk>', article_update_view, name='article_update'), path('edit/<int:pk>', article_update_view, name='article_update'),
path('author/create', author_create_view, name="add_author"),
path('author/list', author_list_view, name="list_author")
] ]
\ No newline at end of file
from django.shortcuts import render from django.shortcuts import render
from article.models import Article from article.models import Article, Author
from django.shortcuts import redirect from django.shortcuts import redirect
from django.http import HttpResponseNotFound from django.http import HttpResponseNotFound
from datetime import datetime from datetime import datetime
...@@ -11,17 +11,20 @@ def index_view(request): ...@@ -11,17 +11,20 @@ def index_view(request):
def article_create_view(request): def article_create_view(request):
if request.method == 'GET': if request.method == 'GET':
return render(request, 'article/create.html') return render(request, 'article/create.html', context={
'authors': Author.objects.all()
})
elif request.method == 'POST': elif request.method == 'POST':
publication_date = datetime.strptime(request.POST.get("publication_date"), "%Y-%m-%dT%H:%M") publication_date = datetime.strptime(request.POST.get("publication_date"), "%Y-%m-%dT%H:%M")
now_time = datetime.now() now_time = datetime.now()
if publication_date < datetime.now(): if publication_date < datetime.now():
return render(request, 'article/create.html', context={ return render(request, 'article/create.html', context={
"error_message": f"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"}) "error_message": f"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"})
author = Author.objects.get(id=request.POST.get('author_id'))
new_article = Article.objects.create( new_article = Article.objects.create(
title=request.POST.get('title'), title=request.POST.get('title'),
content=request.POST.get('content'), content=request.POST.get('content'),
author=request.POST.get('author'), author=author,
publication_date=publication_date publication_date=publication_date
) )
new_article.save() new_article.save()
...@@ -63,3 +66,21 @@ def article_list_view(request): ...@@ -63,3 +66,21 @@ def article_list_view(request):
return render(request, 'article/list.html', context={ return render(request, 'article/list.html', context={
'articles': Article.objects.all() 'articles': Article.objects.all()
}) })
def author_create_view(request):
if request.method == "GET":
return render(request, 'author/create.html')
if request.method == "POST":
new_author = Author.objects.create(
last_name=request.POST.get("last_name"),
name=request.POST.get("name"),
)
new_author.save()
return redirect("list_author")
def author_list_view(request):
return render(request, "author/list.html", context={"authors": Author.objects.all()})
...@@ -19,8 +19,12 @@ ...@@ -19,8 +19,12 @@
<textarea name="content" class="form-control" id="content_input" placeholder="Текст..."></textarea> <textarea name="content" class="form-control" id="content_input" placeholder="Текст..."></textarea>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="author_input" class="form-label">Author</label> <label for="author_input" class="form-label">Author</label>
<input type="text" name="author" class="form-control" id="author_input" placeholder="Автор..."> <select name="author_id" id="author_input">
{% for author in authors %}
<option value="{{ author.id }}">{{ author }}</option>
{% endfor %}
</select>
</div> </div>
<div class="mb-3"> <div class="mb-3">
<label for="publication_date_input" class="form-label">Дата публикации</label> <label for="publication_date_input" class="form-label">Дата публикации</label>
......
{% extends 'base.html' %}
{% block content %}
<div class="row">
<div class="col-md-6">
{% if error_message %}
<div class="alert alert-danger" role="alert">
{{ error_message }}
</div>
{% endif %}
<form action="{% url 'add_author' %}" method="post">
{% csrf_token %}
<div class="mb-3">
<label for="name_input" class="form-label">Имя</label>
<input type="text" name="name" class="form-control" id="name_input" placeholder="Имя...">
</div>
<div class="mb-3">
<label for="last_name_input" class="form-label">Фамилия</label>
<input type="text" name="last_name" class="form-control" id="last_name_input" placeholder="Фамили...">
</div>
<button type="submit" class="btn btn-success">Create</button>
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<div class="row">
<div class="col-md-6">
<div class="list-group">
{% for author in authors %}
<div>
{{ author }}
</div>
{% endfor %}
</div>
</div>
</div>
<a href="{% url 'add_author' %}" class="btn btn-info">Add New Author</a>
{% endblock %}
...@@ -25,6 +25,9 @@ ...@@ -25,6 +25,9 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="{% url 'add_article' %}">Add New Article</a> <a class="nav-link" href="{% url 'add_article' %}">Add New Article</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="{% url 'add_author' %}">Add New Author</a>
</li>
</ul> </ul>
</div> </div>
</div> </div>
......
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