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

lesson 47

parent de1e954d
......@@ -23,4 +23,6 @@ urlpatterns = [
path('', views.index_view, name='index'),
path('articles/add', views.article_create_view, name='articles_add'),
path('articles/<int:id>', views.article_detail_view, name='articles_detail'),
path('articles/<int:id>/update', views.article_update_view, name='articles_update'),
path('articles/<int:id>/delete', views.article_delete_view, name='articles_delete'),
]
No preview for this file type
......@@ -4,19 +4,6 @@
<h1>Create Article</h1>
<form action="{% url 'articles_add' %}" method="post">
{% csrf_token %}
<label>Title:</label><br>
<input type="text" name="title">
<br>
<label>Text:</label><br>
<textarea name="text"></textarea><br>
<label>Author:</label><br>
<input type="text" name="author"><br>
<br>
<input type="submit" value="Send">
{% include 'partial/article_form.html' with btn_text='Create' %}
</form>
{% endblock %}
\ No newline at end of file
......@@ -6,4 +6,14 @@
<h2>{{ article.title }}</h2>
<p>{{ article.text }}</p>
<p>By: {{ article.author }}</p>
<a href="{% url 'articles_update' id=article.id %}" class="btn btn-primary">
Update
</a>
<form action="{% url 'articles_delete' id=article.id %}" method="post" onsubmit="return confirm('Delete?')">
{% csrf_token %}
<button class="btn btn-danger">Delete</button>
</form>
{% endblock %}
\ No newline at end of file
{% extends 'base.html' %}
{% block content %}
<h1>Update Article</h1>
<form action="{% url 'articles_update' id=article.id %}" method="post">
{% include 'partial/article_form.html' %}
</form>
{% endblock %}
\ No newline at end of file
{% csrf_token %}
{% for error in form.non_field_errors %}
<p class="text-danger">{{ error }}</p>
{% endfor %}
<label>Title:</label><br>
<input type="text" name="title" value="{{ article.title }}">
{% for error in form.title.errors %}
<p class="text-danger">{{ error }}</p>
{% endfor %}
<br>
<label>Text:</label><br>
<textarea name="text">{{ article.text }}</textarea><br>
{% for error in form.text.errors %}
<p class="text-danger">{{ error }}</p>
{% endfor %}
<label>Author:</label><br>
<input type="text" name="author" value="{{ article.author }}"><br>
{% for error in form.author.errors %}
<p class="text-danger">{{ error }}</p>
{% endfor %}
<br>
<input type="submit" value="{{ btn_text|default:'Send' }}" class="btn btn-primary">
\ No newline at end of file
from django import forms
from django.forms import widgets
class ArticleForm(forms.Form):
title = forms.CharField(max_length=200, required=True, label='Title')
author = forms.CharField(max_length=40, required=True, label='Author')
text = forms.CharField(
max_length=3000, required=True,
label='Text', widget=widgets.Textarea,
)
from django.shortcuts import render, get_object_or_404, redirect
from webapp.forms import ArticleForm
from webapp.models import Article
# CRUD - Create Read Update Delete
def index_view(request):
context = {'articles': Article.objects.all()}
......@@ -16,14 +20,59 @@ def article_detail_view(request, id):
def article_create_view(request):
if request.method == 'GET':
return render(request, 'articles/create.html')
form = ArticleForm()
return render(request, 'articles/create.html', context={'form': form})
elif request.method == 'POST':
print(request.POST)
article = Article.objects.create(
title=request.POST.get('title'),
text=request.POST.get('text'),
author=request.POST.get('author'),
form = ArticleForm(data=request.POST)
if form.is_valid():
article = Article.objects.create(
title=request.POST.get('title'),
text=request.POST.get('text'),
author=request.POST.get('author'),
)
return redirect('articles_detail', id=article.id)
else:
return render(
request, 'articles/create.html',
context={'form': form},
)
def article_update_view(request, id):
article = get_object_or_404(Article, id=id)
if request.method == 'GET':
form = ArticleForm(initial={
'title': article.title,
'text': article.text,
'author': article.author,
})
return render(
request, 'articles/update.html',
context={'article': article, 'form': form},
)
elif request.method == 'POST':
form = ArticleForm(data=request.POST)
return redirect('articles_detail', id=article.id)
if form.is_valid():
article.title = form.cleaned_data.get('title')
article.author = form.cleaned_data.get('author')
article.text = form.cleaned_data.get('text')
article.save()
return redirect('index')
else:
return render(request, 'articles/update.html', context={'form': form, 'article': article})
def article_delete_view(request, id):
get_object_or_404(Article, id=id).delete()
return redirect('index')
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