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

webinar 22

parent 90aa34ed
......@@ -76,9 +76,13 @@ WSGI_APPLICATION = 'core.wsgi.application'
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "articles",
"USER": "postgres",
"PASSWORD": "postgres",
"HOST": "localhost",
"PORT": 5435
}
}
......
......@@ -22,7 +22,7 @@ from web import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index_view, name='main-page'),
path('articles/add/', views.article_create_view, name='articles-add'),
path('articles/add/', views.ArticleCreateView.as_view(), name='articles-add'),
# /articles/1/ OR /articles/?id=1
# article_details_view(request, id)
path('articles/<int:id>/', views.article_detail_view, name='articles-detail'),
......
No preview for this file type
# Generated by Django 3.2.19 on 2023-06-21 10:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('web', '0003_alter_article_options'),
]
operations = [
migrations.AddField(
model_name='article',
name='status',
field=models.CharField(choices=[['accepted', 'Разрешено'], ['unaccepted', 'Не разрешено'], ['check', 'На проверке']], default='check', max_length=100),
),
]
# Generated by Django 3.2.19 on 2023-06-21 10:17
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('web', '0004_article_status'),
]
operations = [
migrations.AlterField(
model_name='article',
name='status',
field=models.CharField(choices=[('accepted', 'Разрешено'), ('unaccepted', 'Не разрешено'), ('check', 'На проверке')], default='check', max_length=100),
),
]
from django.db import models
from django.db.models import TextChoices
class StatusChoices(TextChoices):
ACCEPTED = 'accepted', 'Разрешено'
UNACCEPTED = 'unaccepted', 'Не разрешено'
CHECK = 'check', 'На проверке'
class Article(models.Model):
......@@ -6,11 +13,18 @@ class Article(models.Model):
# title varchar(200) not null
title = models.CharField(
max_length=200,
null=False, # <null>
blank=False, # ''
null=False, # <null>
blank=False, # ''
verbose_name='Заголовок'
)
# accepted, check, unaccepted
status = models.CharField(
max_length=100,
choices=StatusChoices.choices,
default='check'
)
text = models.TextField(
max_length=3000,
null=False,
......
......@@ -8,6 +8,7 @@
<h2>{{ article.title }}</h2>
<p>{{ article.text }}</p>
<h5>Status: {{ article.status }}</h5>
<h5>By: {{ article.author }}</h5>
<h4>Created: {{ article.created_at }}</h4>
......
......@@ -11,6 +11,16 @@
<label for="titleId" class="form-label">Title</label>
<input type="text" name="title" class="form-control" id="titleId" aria-describedby="title">
</div>
<div class="mb-3">
<label for="statusId" class="form-label">Status</label>
<select name="status" id="statusId" class="form-select">
{% for choice in status_choices %}
<option value="{{ choice.0 }}">{{ choice.1 }}</option>
{% endfor %}
</select>
</div>
<div class="mb-3">
<label for="textId" class="form-label">Text</label>
<textarea name="text" class="form-control" id="textId" cols="30" rows="10"></textarea>
......
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.views import View
from web.models import Article
from web.models import Article, StatusChoices
def index_view(request):
......@@ -13,16 +14,24 @@ def index_view(request):
)
def article_create_view(request):
if request.method == 'GET':
return render(request, 'article_create.html')
# Class-based views
class ArticleCreateView(View):
def get(self, request, *args, **kwargs):
return render(
self.request,
'article_create.html',
context={
'status_choices': StatusChoices.choices
}
)
elif request.method == 'POST':
def post(self, request, *args, **kwargs):
data = request.POST
article_data = {
'title': data.get('title'),
'text': data.get('text'),
'author': data.get('author')
'author': data.get('author'),
'status': data.get('status')
}
article = Article.objects.create(**article_data)
......@@ -30,6 +39,30 @@ def article_create_view(request):
return redirect('articles-detail', id=article.id)
# def article_create_view(request):
# if request.method == 'GET':
# return render(
# request,
# 'article_create.html',
# context={
# 'status_choices': StatusChoices.choices
# }
# )
#
# elif request.method == 'POST':
# data = request.POST
# article_data = {
# 'title': data.get('title'),
# 'text': data.get('text'),
# 'author': data.get('author'),
# 'status': data.get('status')
# }
#
# article = Article.objects.create(**article_data)
#
# return redirect('articles-detail', id=article.id)
# /articles/{id}
def article_detail_view(request, id: int):
article = get_object_or_404(Article, id=id)
......
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