Lesson 36

parent 7949007a
from django.contrib import admin
from article.models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ['id', 'title', 'author', 'created_at']
list_filter = ['author',]
search_fields = ['title', 'content',]
fields = ['title', 'content', 'author', 'created_at', 'updated_at']
readonly_fields = ['created_at', 'updated_at']
admin.site.register(Article, ArticleAdmin)
# Generated by Django 3.2.4 on 2021-06-17 14:04
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=100, verbose_name='Название')),
('content', models.TextField(max_length=3000, verbose_name='Тело')),
('author', models.CharField(max_length=100, verbose_name='Автор')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Дата и время создания')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Дата и время изменения')),
],
),
]
from django.db import models
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='Тело')
author = models.CharField(max_length=100, null=False, blank=False, verbose_name='Автор')
created_at = models.DateTimeField(auto_now_add=True, verbose_name="Дата и время создания")
updated_at = models.DateTimeField(auto_now=True, verbose_name="Дата и время изменения")
def __str__(self):
return f"{self.pk}. {self.title}"
class Meta:
verbose_name='Статья'
verbose_name_plural='Статьи'
\ No newline at end of file
from django.shortcuts import render
from article.models import Article
def index_view(request):
return render(request, 'index.html')
......@@ -8,8 +10,13 @@ def article_create_view(request):
if request.method == 'GET':
return render(request, 'article/create.html')
elif request.method == 'POST':
return render(request, 'article/detail.html', context= {
"title": request.POST.get('title'),
"content": request.POST.get('content'),
"author": request.POST.get('author'),
})
new_article = Article.objects.create(
title=request.POST.get('title'),
content=request.POST.get('content'),
author=request.POST.get('author')
)
new_article.save()
articles_list = Article.objects.all()
return render(request, 'article/list.html', context={
'articles': articles_list
})
\ No newline at end of file
......@@ -105,9 +105,9 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'ru-ru'
TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Almaty'
USE_I18N = True
......
[
{
"model": "article.article",
"pk": 8,
"fields": {
"title": "Статья 1",
"content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"author": "Л. Толстой",
"created_at": "2021-06-17T15:06:16.147Z",
"updated_at": "2021-06-17T15:06:16.147Z"
}
},
{
"model": "article.article",
"pk": 9,
"fields": {
"title": "Статья 2",
"content": "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem IpsumLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
"author": "A. Пушкин",
"created_at": "2021-06-17T15:09:10.625Z",
"updated_at": "2021-06-17T15:15:04.486Z"
}
},
{
"model": "article.article",
"pk": 10,
"fields": {
"title": "Статья 3",
"content": "Asia/AlmatyAsia/AlmatyAsia/AlmatyAsia/AlmatyAsia/AlmatyAsia/AlmatyAsia/Almaty",
"author": "Тургенев",
"created_at": "2021-06-17T15:18:53.966Z",
"updated_at": "2021-06-17T15:18:53.966Z"
}
}
]
asgiref==3.3.4
Django==3.2.4
pkg-resources==0.0.0
pytz==2021.1
sqlparse==0.4.1
......@@ -19,11 +19,11 @@
<input type="text" name="title" class="form-control" id="title_input" placeholder="Название...">
</div>
<div class="mb-3">
<label for="content_input" class="form-label">Title</label>
<label for="content_input" class="form-label">Content</label>
<textarea name="content" class="form-control" id="content_input" placeholder="Текст..."></textarea>
</div>
<div class="mb-3">
<label for="author_input" class="form-label">Title</label>
<label for="author_input" class="form-label">Author</label>
<input type="text" name="author" class="form-control" id="author_input" placeholder="Автор...">
</div>
<button type="submit" class="btn btn-success">Create</button>
......
......@@ -14,9 +14,9 @@
<div class="row">
<div class="col-md-6">
<h2>Статья</h2>
<p>{{ title }}</p>
<p>{{ author }}</p>
<p>{{ content }}</p>
<p>{{ article.title }}</p>
<p>{{ article.author }}</p>
<p>{{ article.content }}</p>
</div>
</div>
</div>
......
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
<title>Document</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<ul>
{% for article in articles %}
<li>{{ article }}</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</body>
</html>
\ 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