asdfasdf

parent 457bc91f
from django import forms
from article.models import Genre
class GenreForm(forms.Form):
title = forms.CharField(
required=True,
max_length=50,
min_length=3,
label="Название жанра")
description = forms.CharField(required=False, max_length=500, label="Описание", widget=forms.Textarea(attrs={'style': 'border: 2px solid red'}))
class Meta:
fields = ['title', 'description']
......@@ -9,6 +9,14 @@ class Author(models.Model):
return f"{self.name} {self.last_name}"
class Genre(models.Model):
title = models.CharField(max_length=50, null=False, blank=False, verbose_name='Название', )
description = models.CharField(max_length=100, null=False, blank=False, verbose_name='Описание')
def __str__(self):
return f"{self.pk}. {self.title}"
class Article(models.Model):
title = models.CharField(max_length=100, null=False, blank=False, verbose_name='Название')
content = models.TextField(max_length=3000, null=False, blank=False, verbose_name='Тело')
......
from django.urls import path
from article.views import \
from article.views.article_views import \
article_create_view, \
article_list_view, \
article_detail_view, \
article_update_view, \
article_update_view
from article.views.author_view import \
author_list_view, \
author_create_view
author_create_view, \
author_update_view
from article.views.genre_view import (
genre_create_view,
genre_list_view
)
urlpatterns = [
path('add/', article_create_view, name='add_article'),
......@@ -15,5 +22,9 @@ urlpatterns = [
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")
path('author/list', author_list_view, name="list_author"),
path('author/<int:pk>/edit', author_update_view, name="update_author"),
path('genre/create', genre_create_view, name="add_genre"),
path('genre/list', genre_list_view, name="list_genre"),
]
......@@ -5,82 +5,8 @@ from django.http import HttpResponseNotFound
from datetime import datetime
def index_view(request):
return render(request, 'index.html')
def article_create_view(request):
if request.method == 'GET':
return render(request, 'article/create.html', context={
'authors': Author.objects.all()
})
elif request.method == 'POST':
publication_date = datetime.strptime(request.POST.get("publication_date"), "%Y-%m-%dT%H:%M")
now_time = datetime.now()
if publication_date < datetime.now():
return render(request, 'article/create.html', context={
"error_message": f"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"})
author = Author.objects.get(id=request.POST.get('author_id'))
new_article = Article.objects.create(
title=request.POST.get('title'),
content=request.POST.get('content'),
author=author,
publication_date=publication_date
)
new_article.save()
return redirect('article_detail', pk=new_article.pk)
def article_detail_view(request, *args, **kwargs):
try:
article = Article.objects.get(pk=kwargs.get('pk'))
except Article.DoesNotExist as e:
return HttpResponseNotFound(f'Статья с id {kwargs.get("pk")} не найдена')
return render(request, 'article/detail.html', context={'article': article})
def article_update_view(request, pk):
article = Article.objects.get(pk=pk)
if request.method == 'GET':
publication_date_format = article.publication_date
article.publication_date = publication_date_format.strftime('%Y-%m-%dT%H:%M')
return render(request, 'article/update.html', context={'article': article})
if request.method == 'POST':
publication_date = datetime.strptime(request.POST.get("publication_date"), "%Y-%m-%dT%H:%M")
now_time = datetime.now()
if publication_date < datetime.now():
return render(request, 'article/update.html', context={
"error_message": f"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"})
article.title = request.POST.get('title'),
print(request.POST.get('title'))
article.content = request.POST.get('content'),
article.author = request.POST.get('author'),
article.publication_date = publication_date
article.save()
return redirect('article_detail', pk=article.pk)
def article_list_view(request):
return render(request, 'article/list.html', context={
'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()})
from datetime import datetime
from django.http import HttpResponseNotFound
from django.shortcuts import render, redirect
from article.models import Author, Article
def index_view(request):
return render(request, 'index.html')
def article_create_view(request):
if request.method == 'GET':
return render(request, 'article/create.html', context={
'authors': Author.objects.all()
})
elif request.method == 'POST':
publication_date = datetime.strptime(request.POST.get("publication_date"), "%Y-%m-%dT%H:%M")
now_time = datetime.now()
if publication_date < datetime.now():
return render(request, 'article/create.html', context={
"error_message": f"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"})
author = Author.objects.get(id=request.POST.get('author_id'))
new_article = Article.objects.create(
title=request.POST.get('title'),
content=request.POST.get('content'),
author=author,
publication_date=publication_date
)
new_article.save()
return redirect('article_detail', pk=new_article.pk)
def article_detail_view(request, *args, **kwargs):
try:
article = Article.objects.get(pk=kwargs.get('pk'))
except Article.DoesNotExist as e:
return HttpResponseNotFound(f'Статья с id {kwargs.get("pk")} не найдена')
return render(request, 'article/detail.html', context={'article': article})
def article_update_view(request, pk):
article = Article.objects.get(pk=pk)
if request.method == 'GET':
publication_date_format = article.publication_date
article.publication_date = publication_date_format.strftime('%Y-%m-%dT%H:%M')
return render(request, 'article/update.html', context={'article': article})
if request.method == 'POST':
publication_date = datetime.strptime(request.POST.get("publication_date"), "%Y-%m-%dT%H:%M")
now_time = datetime.now()
if publication_date < datetime.now():
return render(request, 'article/update.html', context={
"error_message": f"Дата публикации ({publication_date}) не должна быть раньше текущей {now_time}"})
article.title = request.POST.get('title'),
print(request.POST.get('title'))
article.content = request.POST.get('content'),
article.author = request.POST.get('author'),
article.publication_date = publication_date
article.save()
return redirect('article_detail', pk=article.pk)
def article_list_view(request):
return render(request, 'article/list.html', context={
'articles': Article.objects.all()
})
\ No newline at end of file
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from article.models import Author
def author_create_view(request):
if request.method == "GET":
return render(request, 'author/create.html', context={
'link': reverse('add_author'),
'button_title': "Создать"
})
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()})
def author_update_view(request, pk):
if request.method == 'GET':
print(reverse('update_author', kwargs={'pk': pk}))
return render(request, 'author/create.html', context={
'author': get_object_or_404(Author, pk=pk),
'link': reverse('update_author', kwargs={'pk': pk}),
'button_title': "Изменить"
})
if request.method == 'POST':
author = get_object_or_404(Author, pk=pk)
author.name = request.POST.get('name')
author.last_name = request.POST.get('last_name')
errors = dict()
if len(author.name) > 100:
errors["name"] = "Слишком длинное имя автора"
if len(author.last_name) > 100:
errors["last_name"] = "Слишком длинная фамилия автора"
if errors:
return render(request, 'author/create.html', context={
"errors": errors,
'author': author,
'link': reverse('update_author', kwargs={'pk': pk}),
'button_title': "Изменить"})
author.save()
return redirect('list_author')
\ No newline at end of file
from django.shortcuts import render, redirect
from article.models import Genre
from article.forms.genre_form import GenreForm
def genre_create_view(request):
if request.method == 'GET':
form = GenreForm()
return render(request, 'genre/create.html', context={'form': form})
if request.method == 'POST':
form = GenreForm(request.POST)
if form.is_valid():
genre = Genre.objects.create(
title=form.cleaned_data['title'],
description=form.cleaned_data['description']
)
genre.save()
return redirect('list_genre')
return render(request, 'genre/create.html', context={'form': form})
def genre_list_view(request):
return render(request, 'genre/list.html', context={'genres': Genre.objects.all()})
......@@ -15,7 +15,7 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
from article.views import index_view
from article.views.article_views import index_view
urlpatterns = [
path('admin/', admin.site.urls),
......
......@@ -3,22 +3,27 @@
{% 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">
<form action="{{ link }}" 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="Имя...">
{% if errors.name %}
<p style="color: red; font-size: 12px;">{{ errors.name }}</p>
<input type="text" style="border: 2px red solid" name="name" class="form-control" value="{{ author.name }}" id="name_input" placeholder="Имя...">
{% else %}
<input type="text" name="name" class="form-control" value="{{ author.name }}" id="name_input" placeholder="Имя...">
{% endif %}
</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="Фамили...">
{% if errors.last_name %}
<p style="color: red; font-size: 12px;">{{ errors.last_name }}</p>
<input type="text" name="last_name" style="border: 2px red solid" class="form-control" value="{{ author.last_name }}" id="last_name_input" placeholder="Фамилия...">
{% else %}
<input type="text" name="last_name" class="form-control" value="{{ author.last_name }}" id="last_name_input" placeholder="Фамилия...">
{% endif %}
</div>
<button type="submit" class="btn btn-success">Create</button>
<button type="submit" class="btn btn-success">{{ button_title }}</button>
</form>
</div>
</div>
......
......@@ -3,13 +3,19 @@
{% block content %}
<div class="row">
<div class="col-md-6">
<div class="list-group">
<ul class="list-group">
{% for author in authors %}
<li class="list-group-item">
<div>
{{ author }}
</div>
{% endfor %}
<div>
{% url "update_author" author.id as update_link %}
{% include 'partial/update_button.html' with link=update_link %}
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
<a href="{% url 'add_author' %}" class="btn btn-info">Add New Author</a>
......
......@@ -11,27 +11,7 @@
<title>Document</title>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{% url 'index_view' %}">My Blog</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'article_list' %}">Articles</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'add_article' %}">Add New Article</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'add_author' %}">Add New Author</a>
</li>
</ul>
</div>
</div>
</nav>
{% include 'partial/navigation.html' %}
<div class="container">
{% block content %}
......
{% extends 'base.html' %}
{% block content %}
<form action="{% url 'add_genre' %}" method="POST">
{% csrf_token %}
{{ form.non_field_errors }}
<div class="form-group">
{{ form.messages.title }}
<label for="{{ form.title.id_for_label }}">{{ form.title.label }}</label>
{{ form.title }}
</div>
<div class="form-group">
{{ form.messages.description }}
<label for="{{ form.description.id_for_label }}">{{ form.description.label }}</label>
{{ form.description }}
</div>
<input type="submit" value="Создать">
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<div class="row">
<div class="col-md-6">
<ul class="list-group">
{% for genre in genres %}
<li class="list-group-item">
<div>
{{ genre }}
</div>
</li>
{% endfor %}
</ul>
</div>
</div>
<a href="{% url 'add_genre' %}" class="btn btn-info">Add Genre</a>
{% endblock %}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="{% url 'index_view' %}">My Blog</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="navbar-nav">
<li class="nav-item">
<a class="nav-link" href="{% url 'article_list' %}">Articles</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'add_article' %}">Add New Article</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'add_author' %}">Add New Author</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'list_author' %}">Authors</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'list_genre' %}">Genres</a>
</li>
</ul>
</div>
</div>
</nav>
\ No newline at end of file
<a href="{{ link }}" class="btn btn-info">Изменить</a>
\ No newline at end of file
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