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

web 24

parent 76d5ea91
......@@ -22,17 +22,26 @@ from web import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.MainPageRedirectView.as_view()),
path('articles/', views.ArticleIndexView.as_view(), name='main_page'),
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.ArticleDetailView.as_view(), name='articles-detail'),
path('articles/<int:id>/edit', views.ArticleUpdateView.as_view(), name='article_update'),
path('articles/<int:id>/delete', views.ArticleDeleteView.as_view(), name='delete_article'),
path(
'articles/<int:article_id>/comments/',
views.ArticleCommentCreateView.as_view(),
name='article_comment_create'
'comments/add/',
views.CommentCreateView.as_view(),
name='comment_create'
),
path('articles/<int:id>/delete', views.ArticleDeleteView.as_view(), name='delete_article')
path(
'comments/<int:id>/edit',
views.CommentUpdateView.as_view(),
name='comment_update'
),
path(
'comments/<int:id>/delete',
views.CommentDeleteView.as_view(),
name='comment_delete'
)
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
......@@ -32,13 +32,14 @@ class ArticleModelForm(forms.ModelForm):
return cleaned_data
class ArticleCommentModelForm(forms.ModelForm):
class CommentModelForm(forms.ModelForm):
class Meta:
model = Comment
fields = ['author', 'text']
fields = ['author', 'text', 'article']
widgets = {
'text': forms.TextInput(attrs={'class': 'form-control mb-3'}),
'author': forms.TextInput(attrs={'class': 'form-control mb-3'}),
'article': forms.HiddenInput()
}
......
......@@ -26,21 +26,36 @@
<hr>
<a href="{% url 'article_update' article.pk %}" class="btn btn-primary mt-4 me-3">Edit</a>
<form action="{% url 'delete_article' article.pk %}" method="POST" onsubmit="confirm('Are you sure?')">
<form action="{% url 'delete_article' article.pk %}" method="POST" onsubmit="return confirm('Are you sure?')">
{% csrf_token %}
<button class="btn btn-danger mt-4">Delete</button>
</form>
<h3 class="mt-4">Comments:</h3>
<div class="comments-list">
<form action="{% url 'article_comment_create' article_id=article.id %}" method="POST">
<form action="{% url 'comment_create' %}" method="POST">
{% csrf_token %}
{% include 'partial/form.html' with button_text='add' %}
<input type="hidden" name="article" value="{{ article.id }}">
</form>
{% for comment in comments %}
<div class="card my-3">
<div class="card-header">
<div class="card-header d-flex justify-content-between">
{{ comment.author }}
<div class="d-flex justify-content-between">
<a href="{% url 'comment_update' id=comment.id %}" class="me-5">
<i class="fa fa-regular fa-pen-to-square"></i>
</a>
<form action="{% url 'comment_delete' id=comment.id %}" method="POST" onsubmit="return confirm('Are you sure?')">
{% csrf_token %}
<button type="submit" class="border-0 text-danger"><i class="fa-solid fa-trash"></i></button>
</form>
</div>
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
......@@ -52,5 +67,6 @@
{% empty %}
<p>No comments yet.</p>
{% endfor %}
</div>
{% endblock %}
......@@ -6,7 +6,7 @@
{% block content %}
<form action="{% url 'article_update' article.pk %}" method="post">
{% include 'partial/article_form.html' with button_text='Update' abc='123' %}
{% include 'partial/form.html' with button_text='Update' abc='123' %}
</form>
{% endblock %}
......@@ -6,7 +6,7 @@
<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"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha512-iecdLmaskl7CVkqkXNQ/ZH/XLlvWZOJyj7Yy7tcenmpD1ypASozpmT/E0iPtmFIB46ZmdtAc9eNBvH0H/ZpiBw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
<title>Title</title>
</head>
......
{% extends 'base.html' %}
{% load static %}
{% block title %}
<h1 class="text-center my-4">{{ title }}</h1>
{% endblock %}
{% block content %}
<form action="" method="post">
{% csrf_token %}
{% include 'partial/form.html' %}
</form>
{% endblock %}
{% csrf_token %}
<!-- <div class="mb-3">
<label for="titleId" class="form-label">Title</label>
{% if errors.title %}
<p class="form-error">{{ errors.title }}</p>
<input type="text" name="title" class="form-control field-error" id="titleId" aria-describedby="title" value="{{ article.title }}">
{%else%}
<input type="text" name="title" class="form-control" id="titleId" aria-describedby="title" value="{{ article.title }}">
{%endif%}
</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 %}
{% if article.status == choice.0 %}
<option value="{{ choice.0 }}" selected>{{ choice.1 }}</option>
{% else %}
<option value="{{ choice.0 }}">{{ choice.1 }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<div class="mb-3">
<label for="textId" class="form-label">Text</label>
{% if errors.text %}
<p class="form-error">{{ errors.text }}</p>
<textarea name="text" class="form-control field-error" id="textId" cols="30" rows="10">{{article.text}}</textarea>
{% else %}
<textarea name="text" class="form-control" id="textId" cols="30" rows="10">{{article.text}}</textarea>
{%endif%}
</div>
<div class="mb-3">
<label for="authorId" class="form-label">Author</label>
{% if errors.author %}
<p class="form-error">{{ errors.author }}</p>
<input type="text" name="author" class="form-control field-error" id="authorId" aria-describedby="author" value="{{article.author}}">
{%else%}
<input type="text" name="author" class="form-control" id="authorId" aria-describedby="author" value="{{article.author}}">
{%endif%}
</div> -->
{% for error in form.non_field_errors %}
<p class="form-error">{{error}}</p>
{%endfor%}
<p><label for="{{form.title.id_for_label}}">{{form.title.label}}</label></p>
{% for error in form.title.errors %}
<p class="form-error"> {{ error }} </p>
{% endfor %}
<p>{{ form.title }}</p>
<p><label for="{{form.author.id_for_label}}">{{form.author.label}}</label></p>
{% for error in form.author.errors %}
<p class="form-error"> {{ error }} </p>
{% endfor %}
<p>{{ form.author }}</p>
<p><label for="{{form.status.id_for_label}}">{{form.status.label}}</label></p>
{% for error in form.status.errors %}
<p class="form-error"> {{ error }} </p>
{% endfor %}
<p>{{ form.status }}</p>
<p><label for="{{form.text.id_for_label}}">{{form.text.label}}</label></p>
{% for error in form.text.errors %}
<p class="form-error"> {{ error }} </p>
{% endfor %}
<p>{{ form.text }}</p>
<button type="submit" class="btn btn-primary">{{ button_text }}</button>
\ No newline at end of file
......@@ -13,4 +13,9 @@
{{ field }}
{% endfor %}
{% for field in form.hidden_fields %}
{{ field }}
{% endfor %}
<button type="submit" class="btn btn-success">{{ button_text }}</button>
from django.views.generic import RedirectView
from web.views.article import (
ArticleDetailView,
ArticleIndexView,
ArticleUpdateView,
ArticleCreateView,
ArticleDeleteView
)
from web.views.comment import (
CommentCreateView,
CommentUpdateView,
CommentDeleteView
)
class MainPageRedirectView(RedirectView):
pattern_name = 'main_page'
from urllib.parse import urlencode
from django.shortcuts import get_object_or_404
from django.urls import reverse, reverse_lazy
from django.db.models import Q
from django.views.generic import (
......@@ -11,12 +10,12 @@ from django.views.generic import (
UpdateView,
DeleteView
)
from web.forms import ArticleModelForm, SearchForm, ArticleCommentModelForm
from web.models import Article, Comment
from web.forms import ArticleModelForm, SearchForm, CommentModelForm
from web.models import Article
class ArticleIndexView(ListView):
template_name = 'index.html'
template_name = 'article/index.html'
context_object_name = 'articles'
model = Article
ordering = ['-created_at']
......@@ -52,12 +51,8 @@ class ArticleIndexView(ListView):
return self.form.cleaned_data.get('search')
class MainPageRedirectView(RedirectView):
pattern_name = 'main_page'
class ArticleCreateView(CreateView):
template_name = 'article_create.html'
template_name = 'article/article_create.html'
model = Article
form_class = ArticleModelForm
......@@ -65,28 +60,12 @@ class ArticleCreateView(CreateView):
return reverse('articles-detail', kwargs={'id': self.object.id})
class ArticleCommentCreateView(CreateView):
model = Comment
form_class = ArticleCommentModelForm
def dispatch(self, request, *args, **kwargs):
self.article = get_object_or_404(Article, id=self.kwargs.get('article_id'))
return super().dispatch(request, *args, **kwargs)
def form_valid(self, form):
form.instance.article = self.article
return super().form_valid(form)
def get_success_url(self):
return reverse('articles-detail', kwargs={'id': self.article.id})
class ArticleDetailView(DetailView):
template_name = 'article_detail.html'
template_name = 'article/article_detail.html'
model = Article
context_object_name = 'article'
pk_url_kwarg = 'id'
extra_context = {'form': ArticleCommentModelForm}
extra_context = {'form': CommentModelForm}
def get_context_data(self, **kwargs):
return super().get_context_data(
......@@ -97,7 +76,7 @@ class ArticleDetailView(DetailView):
class ArticleUpdateView(UpdateView):
model = Article
template_name = 'update.html'
template_name = 'article/update.html'
form_class = ArticleModelForm
context_object_name = 'article'
pk_url_kwarg = 'id'
......@@ -108,7 +87,7 @@ class ArticleUpdateView(UpdateView):
class ArticleDeleteView(DeleteView):
model = Article
template_name = 'delete.html'
template_name = 'article/delete.html'
context_object_name = 'article'
success_url = reverse_lazy('main_page')
pk_url_kwarg = 'id'
from django.urls import reverse
from django.views.generic import (
CreateView,
UpdateView,
DeleteView
)
from web.forms import CommentModelForm
from web.models import Comment
class CommentCreateView(CreateView):
model = Comment
template_name = 'comment/create_update.html'
form_class = CommentModelForm
def get_success_url(self):
return reverse('articles-detail', kwargs={'id': self.object.article.id})
class CommentUpdateView(UpdateView):
model = Comment
template_name = 'comment/create_update.html'
context_object_name = 'comment'
pk_url_kwarg = 'id'
form_class = CommentModelForm
extra_context = {'button_text': 'Update', 'title': 'Update comment'}
def get_initial(self):
return self.object.__dict__
def get_success_url(self):
return reverse('articles-detail', kwargs={'id': self.object.article.id})
class CommentDeleteView(DeleteView):
model = Comment
pk_url_kwarg = 'id'
def get_success_url(self):
return reverse('articles-detail', kwargs={'id': self.object.article.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