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

lesson 44

parent 0a9bd1fa
......@@ -23,5 +23,5 @@ urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index_view),
path('articles/add/', views.article_create_view, name='articles-add'),
path('articles/<str:title>', views.article_detail_view, name='articles-detail'),
path('articles/<int:id>/', views.article_detail_view, name='articles-detail'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
No preview for this file type
from django.contrib import admin
# Register your models here.
from web.models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ['id', 'title', 'author', 'created_at']
list_filter = ['author']
search_fields = ['title', 'text']
fields = ['title', 'author', 'text', 'created_at', 'updated_at']
readonly_fields = ['created_at', 'updated_at']
admin.site.register(Article, ArticleAdmin)
[
{
"model": "web.article",
"pk": 2,
"fields": {
"title": "Война и Мир",
"text": "Много текста",
"author": "Л. Толстой",
"created_at": "2023-06-15T11:08:41.578Z",
"updated_at": "2023-06-15T11:08:41.578Z"
}
},
{
"model": "web.article",
"pk": 3,
"fields": {
"title": "Анегин",
"text": "Евгений Анегин",
"author": "А. Пушкин",
"created_at": "2023-06-15T11:09:39.149Z",
"updated_at": "2023-06-15T11:09:39.150Z"
}
},
{
"model": "web.article",
"pk": 1,
"fields": {
"title": "У лукоморья",
"text": "У лукоморья дуб зеленый",
"author": "А. Пушкин",
"created_at": "2023-06-15T11:11:36.213Z",
"updated_at": "2023-06-15T11:20:02.767Z"
}
},
{
"model": "web.article",
"pk": 7,
"fields": {
"title": "Бородино",
"text": "Скажи ка дядя ведь не даром ...",
"author": "М. Лермонтов",
"created_at": "2023-06-15T11:21:23.882Z",
"updated_at": "2023-06-15T11:21:34.525Z"
}
}
]
[
{
"model": "auth.permission",
"pk": 1,
"fields": {
"name": "Can add log entry",
"content_type": 1,
"codename": "add_logentry"
}
},
{
"model": "auth.permission",
"pk": 2,
"fields": {
"name": "Can change log entry",
"content_type": 1,
"codename": "change_logentry"
}
},
{
"model": "auth.permission",
"pk": 3,
"fields": {
"name": "Can delete log entry",
"content_type": 1,
"codename": "delete_logentry"
}
},
{
"model": "auth.permission",
"pk": 4,
"fields": {
"name": "Can view log entry",
"content_type": 1,
"codename": "view_logentry"
}
},
{
"model": "auth.permission",
"pk": 5,
"fields": {
"name": "Can add permission",
"content_type": 2,
"codename": "add_permission"
}
},
{
"model": "auth.permission",
"pk": 6,
"fields": {
"name": "Can change permission",
"content_type": 2,
"codename": "change_permission"
}
},
{
"model": "auth.permission",
"pk": 7,
"fields": {
"name": "Can delete permission",
"content_type": 2,
"codename": "delete_permission"
}
},
{
"model": "auth.permission",
"pk": 8,
"fields": {
"name": "Can view permission",
"content_type": 2,
"codename": "view_permission"
}
},
{
"model": "auth.permission",
"pk": 9,
"fields": {
"name": "Can add group",
"content_type": 3,
"codename": "add_group"
}
},
{
"model": "auth.permission",
"pk": 10,
"fields": {
"name": "Can change group",
"content_type": 3,
"codename": "change_group"
}
},
{
"model": "auth.permission",
"pk": 11,
"fields": {
"name": "Can delete group",
"content_type": 3,
"codename": "delete_group"
}
},
{
"model": "auth.permission",
"pk": 12,
"fields": {
"name": "Can view group",
"content_type": 3,
"codename": "view_group"
}
},
{
"model": "auth.permission",
"pk": 13,
"fields": {
"name": "Can add user",
"content_type": 4,
"codename": "add_user"
}
},
{
"model": "auth.permission",
"pk": 14,
"fields": {
"name": "Can change user",
"content_type": 4,
"codename": "change_user"
}
},
{
"model": "auth.permission",
"pk": 15,
"fields": {
"name": "Can delete user",
"content_type": 4,
"codename": "delete_user"
}
},
{
"model": "auth.permission",
"pk": 16,
"fields": {
"name": "Can view user",
"content_type": 4,
"codename": "view_user"
}
},
{
"model": "auth.permission",
"pk": 17,
"fields": {
"name": "Can add content type",
"content_type": 5,
"codename": "add_contenttype"
}
},
{
"model": "auth.permission",
"pk": 18,
"fields": {
"name": "Can change content type",
"content_type": 5,
"codename": "change_contenttype"
}
},
{
"model": "auth.permission",
"pk": 19,
"fields": {
"name": "Can delete content type",
"content_type": 5,
"codename": "delete_contenttype"
}
},
{
"model": "auth.permission",
"pk": 20,
"fields": {
"name": "Can view content type",
"content_type": 5,
"codename": "view_contenttype"
}
},
{
"model": "auth.permission",
"pk": 21,
"fields": {
"name": "Can add session",
"content_type": 6,
"codename": "add_session"
}
},
{
"model": "auth.permission",
"pk": 22,
"fields": {
"name": "Can change session",
"content_type": 6,
"codename": "change_session"
}
},
{
"model": "auth.permission",
"pk": 23,
"fields": {
"name": "Can delete session",
"content_type": 6,
"codename": "delete_session"
}
},
{
"model": "auth.permission",
"pk": 24,
"fields": {
"name": "Can view session",
"content_type": 6,
"codename": "view_session"
}
},
{
"model": "auth.permission",
"pk": 25,
"fields": {
"name": "Can add article",
"content_type": 7,
"codename": "add_article"
}
},
{
"model": "auth.permission",
"pk": 26,
"fields": {
"name": "Can change article",
"content_type": 7,
"codename": "change_article"
}
},
{
"model": "auth.permission",
"pk": 27,
"fields": {
"name": "Can delete article",
"content_type": 7,
"codename": "delete_article"
}
},
{
"model": "auth.permission",
"pk": 28,
"fields": {
"name": "Can view article",
"content_type": 7,
"codename": "view_article"
}
},
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "pbkdf2_sha256$260000$cUTyu2bXGpA5G3OvGNIbrA$91PaHsudIfSxWq4gp8iQH7XtH5HW7TMgPXt0lNRkXYo=",
"last_login": "2023-06-05T10:42:44.554Z",
"is_superuser": true,
"username": "davidlee",
"first_name": "",
"last_name": "",
"email": "",
"is_staff": true,
"is_active": true,
"date_joined": "2023-06-05T10:40:57.228Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 2,
"fields": {
"password": "pbkdf2_sha256$260000$LmPUwtCla9xfjukIydXWT4$NBJcl2eZdjvwhRaDfiNXgX2Fj/HJzTroyfvMsRg746s=",
"last_login": null,
"is_superuser": false,
"username": "admin",
"first_name": "",
"last_name": "",
"email": "",
"is_staff": false,
"is_active": true,
"date_joined": "2023-06-05T10:43:18.831Z",
"groups": [],
"user_permissions": []
}
},
{
"model": "auth.user",
"pk": 3,
"fields": {
"password": "pbkdf2_sha256$260000$GJw0btzLKy9YEdrNoRScps$xvfyVJqSxI+06Qd46osGoeQhk/1Q4ffdIryjplvrR1Y=",
"last_login": "2023-06-15T11:17:33.472Z",
"is_superuser": true,
"username": "root",
"first_name": "",
"last_name": "",
"email": "root@root.root",
"is_staff": true,
"is_active": true,
"date_joined": "2023-06-15T11:17:26.554Z",
"groups": [],
"user_permissions": []
}
}
]
# Generated by Django 3.2.19 on 2023-06-15 10:30
from django.db import migrations, models
# squash migrations
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')),
('test', models.CharField(max_length=200, verbose_name='Заголовок')),
('title', models.CharField(max_length=200, verbose_name='Заголовок')),
('text', models.TextField(max_length=3000, verbose_name='Текст')),
('author', models.CharField(default='Unknown', max_length=100, verbose_name='Автор')),
('created_at', models.DateTimeField(auto_now_add=True, verbose_name='Дата создания')),
('updated_at', models.DateTimeField(auto_now=True, verbose_name='Дата обновления')),
],
),
]
# Generated by Django 3.2.19 on 2023-06-15 10:41
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('web', '0001_initial'),
]
operations = [
migrations.RemoveField(
model_name='article',
name='test',
),
]
# Generated by Django 3.2.19 on 2023-06-15 11:27
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('web', '0002_remove_article_test'),
]
operations = [
migrations.AlterModelOptions(
name='article',
options={'verbose_name': 'статью', 'verbose_name_plural': 'статьи'},
),
]
from django.db import models
# Create your models here.
class Article(models.Model):
# CharField = varchar
# title varchar(200) not null
title = models.CharField(
max_length=200,
null=False, # <null>
blank=False, # ''
verbose_name='Заголовок'
)
text = models.TextField(
max_length=3000,
null=False,
blank=False,
verbose_name='Текст'
)
author = models.CharField(
max_length=100,
null=False,
blank=False,
default='Unknown',
verbose_name='Автор'
)
created_at = models.DateTimeField(
auto_now_add=True,
verbose_name='Дата создания'
)
updated_at = models.DateTimeField(
auto_now=True,
verbose_name='Дата обновления'
)
class Meta:
verbose_name = 'статью'
verbose_name_plural = 'статьи'
def __str__(self):
return f'{self.author}: {self.title}'
......@@ -7,10 +7,13 @@
<body>
<h1>Article</h1>
<h2>{{ title }}</h2>
<h2>{{ article.title }}</h2>
<p>{{ content }}</p>
<h5>By: {{ author }}</h5>
<p>{{ article.text }}</p>
<h5>By: {{ article.author }}</h5>
<h4>Created: {{ article.created_at }}</h4>
<h4>Updated: {{ article.updated_at }}</h4>
</body>
</html>
\ No newline at end of file
......@@ -13,7 +13,7 @@
<input type="text" name="title"/><br/>
<label>Text:</label><br/>
<textarea name="content"></textarea><br/>
<textarea name="text"></textarea><br/>
<label>Author:</label><br/>
<input type="text" name="author"/><br/>
......
......@@ -8,23 +8,27 @@
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 rel="stylesheet" href="{% static 'css/styles.css' %}">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.1/css/bootstrap.min.css" integrity="sha512-Ez0cGzNzHR1tYAv56860NLspgUGuQw16GiOOp/I2LuTmpSK9xDXlgJz3XN4cnpXWDmkNBKXR/VDMTCnAaEooxA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<title>Hello</title>
</head>
<body>
<div class="container">
<p>This is my first Django project!</p>
<h1>{{ greetings|default:"Hello World!" }}</h1>
{{ 'This is amazing day with a great people'|wordwrap:10|linebreaksbr }}
<form action="" method="post">
{% csrf_token %}
<input type="text" name="name">
<input type="text" name="surname">
<input type="submit" value="Send">
</form>
<p>Hello World!</p>
<h1>Articles</h1>
<a href="{% url 'articles-add' %}" class="btn btn-primary">Создать</a>
{% for article in articles %}
<br>
<hr>
<br>
<h2>{{ article.title }}</h2>
<p>
<a href="{% url 'articles-detail' id=article.id %}">
Подробнее
</a>
</p>
<br>
<hr>
<br>
{% endfor %}
</div>
</body>
</html>
\ No newline at end of file
from django.shortcuts import render, redirect
from web.models import Article
def index_view(request):
return render(
request, 'index.html', context={
'greetings': 'Hello AP-11!'
'articles': Article.objects.order_by('title')
}
)
......@@ -14,17 +16,19 @@ def article_create_view(request):
return render(request, 'article_create.html')
elif request.method == 'POST':
print(request.POST)
data = request.POST
context = {
article_data = {
'title': data.get('title'),
'content': data.get('content'),
'text': data.get('text'),
'author': data.get('author')
}
return redirect('articles-detail', title=data.get('title'))
article = Article.objects.create(**article_data)
return redirect('articles-detail', id=article.id)
# /articles/{id}
def article_detail_view(request, title: str):
return render(request, 'article.html', context={'title': title})
def article_detail_view(request, id: int):
article = Article.objects.get(id=id)
return render(request, 'article.html', context={'article': article})
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