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

lesson 44

parent 28b09bb4
......@@ -20,6 +20,7 @@ from webapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('123', views.index_view),
path('articles/add', views.article_create_view)
path('', views.index_view),
path('articles/add', views.article_create_view),
path('articles/detail', views.article_detail_view),
]
No preview for this file type
[
{
"model": "webapp.article",
"pk": 2,
"fields": {
"title": "test",
"text": "test",
"author": "Name",
"created_at": "2023-12-28T14:03:12.351Z",
"updated_at": "2023-12-28T14:03:12.351Z"
}
},
{
"model": "webapp.article",
"pk": 3,
"fields": {
"title": "title 3",
"text": "text 2",
"author": "author",
"created_at": "2023-12-28T14:07:01.554Z",
"updated_at": "2023-12-28T14:07:01.558Z"
}
},
{
"model": "webapp.article",
"pk": 4,
"fields": {
"title": "123123",
"text": "fdsagfhsgjhdkgjhjghfgtdrfdewsdfghj",
"author": "Неизвестный автор",
"created_at": "2023-12-28T14:14:22.018Z",
"updated_at": "2023-12-28T14:14:22.018Z"
}
},
{
"model": "auth.user",
"pk": 1,
"fields": {
"password": "pbkdf2_sha256$260000$lq4ksTNG7r59t43pWudHUh$x7l77+SRwPzLJGjrmRiF7Q2f0ZoOsoa0M1jmnjaYOp4=",
"last_login": "2023-12-28T14:10:43.272Z",
"is_superuser": true,
"username": "admin",
"first_name": "john",
"last_name": "doe",
"email": "admin@gmail.com",
"is_staff": true,
"is_active": true,
"date_joined": "2023-12-18T14:33:11Z",
"groups": [
1
],
"user_permissions": [
1,
2,
3,
4,
9,
10,
11,
12,
5
]
}
}
]
......@@ -6,12 +6,13 @@
<title>Document</title>
</head>
<body>
<a href="/">Вернуться на главную</a>
<h1>New Article</h1>
<h1>Article</h1>
<h2>{{ title }}</h2>
<p>{{ text }}</p>
<p>By: {{ author }}</p>
<h2>{{ article.title }}</h2>
<p>{{ article.text }}</p>
<p>By: {{ article.author }}</p>
</body>
</html>
\ No newline at end of file
......@@ -12,9 +12,15 @@
<body>
<div class="container">
<h1>This is my first Django project!</h1>
<button class="btn btn-success">bootstrap connected</button>
<img src="{% static "img/spider_men.jpg" %}" alt="">
<h1>Articles</h1>
<p><a href="/articles/add">Добавить</a></p>
{% for article in articles %}
<h2>{{ article.title }}</h2>
<a href="/articles/detail?id={{ article.id }}">Подробнее...</a>
<hr>
{% endfor %}
</div>
</body>
......
from django.contrib import admin
from webapp import models
# Register your models here.
# admin.site.register(models.Article, ArticleAdmin)
@admin.register(models.Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ('id', 'title', 'author', 'created_at')
list_filter = ('author', 'created_at')
search_fields = ('title', 'text')
fields = ('title', 'author', 'text', 'created_at', 'updated_at')
readonly_fields = ('created_at', 'updated_at')
# Generated by Django 3.2.23 on 2023-12-28 13:46
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=200, verbose_name='Заголовок')),
('text', models.TextField(verbose_name='Текст')),
('author', models.CharField(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='Дата создания')),
],
),
]
from django.db import models
# Create your models here.
class Article(models.Model):
title = models.CharField(
max_length=200, null=False, blank=False, verbose_name='Заголовок',
)
text = models.TextField(
null=False, blank=False, verbose_name='Текст',
)
author = models.CharField(
max_length=40, null=False, blank=False, default='Неизвестный автор',
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.id} {self.title}'
from django.shortcuts import render
from webapp.models import Article
# MVC - Model View Controller
# MTV - Model Template View
# <QueryDict: {'hello': ['world1', 'world2']}>
# GET - получение http://localhost:1027/123?hello=world
# http://localhost:1027/123?hello=world1&hello=world2
# POST - действие
def index_view(request):
context = {'articles': Article.objects.all()}
return render(request, 'index.html', context)
def index_view(request):
return render(request, 'index.html')
def article_detail_view(request):
# /articles/detail?id=1
article_id = request.GET.get('id')
context = {'article': Article.objects.get(id=article_id)}
# <QueryDict: {'csrfmiddlewaretoken': ['NSGuE2NGozjOQoH6d8d7CsBvW8jTmF57Hjbooz6VMoXTEIQkEynKqyeqiddle6e3'],
# 'title': ['article 1'], 'text': ['rewgsdgadgdsagdsagsdag'],
# 'author': ['anonymous author']}>
return render(request, 'articles/detail.html', context)
def article_create_view(request):
......@@ -26,10 +22,12 @@ def article_create_view(request):
elif request.method == 'POST':
print(request.POST)
context = {
'title': request.POST.get('title'),
'text': request.POST.get('text'),
'author': request.POST.get('author'),
}
article = Article.objects.create(
title=request.POST.get('title'),
text=request.POST.get('text'),
author=request.POST.get('author'),
)
context = {'article': article}
return render(request, 'articles/detail.html', context)
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