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

webinar 24

parent 8592d5b4
......@@ -11,15 +11,12 @@
body {
font-family: Arial, sans-serif;
font-size: 16px;
color: #444444;
background-color: pink;
}
.container {
margin: 0 auto;
width: 100%;
max-width: 900px;
text-align: center;
}
.field-error {
......
......@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django_extensions',
'web'
]
......@@ -134,3 +135,6 @@ STATICFILES_DIRS = [
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
SHELL_PLUS = "bpython"
from django import forms
from django.forms import widgets
from web.models import Article
class ArticleForm(forms.Form):
title = forms.CharField(max_length=200, required=True, label='Title')
author = forms.CharField(max_length=40, required=True, label='Author')
status = forms.CharField(required=True, label='Status')
text = forms.CharField(max_length=3000, required=True, label='Text', widget=widgets.Textarea)
\ No newline at end of file
text = forms.CharField(max_length=3000, required=True, label='Text', widget=widgets.Textarea)
class ArticleModelForm(forms.ModelForm):
class Meta:
model = Article
fields = ('title', 'author', 'status', 'text', 'tags')
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control mb-3'}),
'author': forms.TextInput(attrs={'class': 'form-control mb-3'}),
'status': forms.Select(attrs={'class': 'form-select mb-3'}),
'text': forms.Textarea(attrs={'class': 'form-control mb-3'}),
'tags': forms.CheckboxSelectMultiple(attrs={'class': 'mb-3'})
}
{% extends 'base.html' %}
{% block title %}
<h1>Article checkout</h1>
{% endblock %}
{% block content %}
<h2>{{ article.title }}</h2>
<p>{{ article.text }}</p>
<h5>Status: {{ article.status }}</h5>
<h5>By: {{ article.author }}</h5>
<h4>Created: {{ article.created_at }}</h4>
<h4>Updated: {{ article.updated_at }}</h4>
<a href="{% url 'article_update' article.pk %}">Edit</a>
<a href="{% url 'delete_article' article.pk %}">Delete</a>
{% endblock %}
......@@ -3,12 +3,13 @@
{% load static %}
{% block title %}
<h1>Create Article</h1>
<h1 class="text-center my-4">Create Article</h1>
{% endblock %}
{% block content %}
<form action="/articles/add/" method="post">
{% include 'partial/article_form.html' with button_text='Create' %}
{% csrf_token %}
{% include 'partial/form.html' with button_text='Create' %}
</form>
{% endblock %}
{% extends 'base.html' %}
{% block title %}
<h2 class="my-4">Article checkout</h2>
<hr>
{% endblock %}
{% block content %}
<div><b>Title: </b>{{ article.title }}</div>
<b>Tags:</b>
<ol>
{% for tag in article.tags.all %}
<li>{{ tag.name }}</li>
{% endfor %}
</ol>
<div><b>Text: </b>{{ article.text }}</div>
<div><b>Status: </b>{{ article.status }}</div>
<div><b>By: </b>{{ article.author }}</div>
<div><b>Created: </b>{{ article.created_at }}</div>
<div><b>Updated: </b>{{ article.updated_at }}</div>
<hr>
<a href="{% url 'article_update' article.pk %}" class="btn btn-primary mt-4 me-3">Edit</a>
<a href="{% url 'delete_article' article.pk %}" class="btn btn-danger mt-4">Delete</a>
{% endblock %}
{% for error in form.non_field_errors %}
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% for field in form.visible_fields %}
{% if field.errors %}
{% for error in field.errors %}
<div class="alert alert-danger">{{ error }}</div>
{% endfor %}
{% endif %}
{{ field.label }}
{{ field }}
{% endfor %}
<button type="submit" class="btn btn-success">{{ button_text }}</button>
from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse
from django.views.generic import View, TemplateView, RedirectView
from web.forms import ArticleForm
from web.forms import ArticleForm, ArticleModelForm
from web.models import Article, StatusChoices
def index_view(request):
return render(
request, 'index.html', context={
'articles': Article.objects.order_by('title'),
'articles': Article.objects.order_by('-created_at'),
'reverse': reverse('articles-detail', kwargs={'id': 1})
}
)
......@@ -21,7 +21,7 @@ class MainPageRedirectView(RedirectView):
# Class-based views
class ArticleCreateView(View):
def get(self, request, *args, **kwargs):
form = ArticleForm()
form = ArticleModelForm()
return render(
self.request,
......@@ -35,15 +35,10 @@ class ArticleCreateView(View):
def post(self, request, *args, **kwargs):
errors = {}
data = request.POST
form = ArticleForm(data=data)
form = ArticleModelForm(data=data)
if form.is_valid():
article = Article.objects.create(
title=form.cleaned_data['title'],
status=form.cleaned_data['status'],
text=form.cleaned_data['text'],
author=form.cleaned_data['author'],
)
return redirect(redirect('articles-detail', id=article.id))
article = form.save()
return redirect('articles-detail', id=article.id)
else:
return render(self.request, 'article_create.html', context={'form': form, 'status_choices': StatusChoices.choices})
......@@ -51,11 +46,11 @@ class ArticleCreateView(View):
# /articles/{id}
def article_detail_view(request, id: int):
article = get_object_or_404(Article, id=id)
return render(request, 'article.html', context={'article': article})
return render(request, 'article_detail.html', context={'article': article})
class ArticleDetailView(TemplateView):
template_name = 'article.html'
template_name = 'article_detail.html'
def get_context_data(self, **kwargs):
article = get_object_or_404(Article, id=kwargs.get('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