models.py
views.py
Заранее спасибо за понимание.
Python:
@property
def split1(self):
length = len(self.paragraph)
p1, p2 = self.paragraph[:length // 2], self.paragraph[length // 2:]
return p1
@property
def split2(self):
length = len(self.paragraph)
p1, p2 = self.paragraph[:length // 2], self.paragraph[length // 2:]
return p2
views.py
Python:
@method_decorator(login_required, name='dispatch')
class CreateArticleView(View):
template_name = "article/forms/ArticleCreate.html"
form_class = ArticleCreateForm
context = dict()
# renders the create form
def get(self, request):
form = self.form_class()
self.context = self.get_context(form)
return render(request, self.template_name, self.context)
# creates and saves the Article object
def post(self, request):
form = self.form_class(request.POST, request.FILES)
self.context = self.get_context(form)
if form.is_valid():
article = form.save(commit=False)
article.author = request.user.profile
article.save()
# add tags
for tag in form.cleaned_data.get("tags"):
article.tags.add(tag)
counter = 1
self.create_paragraphs(request=request, article=article, counter=counter)
return redirect('articles-mode', filter_mode="All")
else:
return render(request, self.template_name, self.context)
# noinspection PyMethodMayBeStatic
# saves the article paragraphs one by one
# looping the request.POST dict
def create_paragraphs(self, request, article, counter):
for key, value in request.POST.items():
if key == f"text_area_{counter}":
p = Paragraph(type="1line", paragraph=value, article=article)
p.save()
counter += 1
if key == f"text_area_split_{counter}":
p = Paragraph(type="split", paragraph=value, article=article)
p.save()
counter += 1
return None
# gets the context dic and adds the form into it
def get_context(self, form):
self.context['form'] = form
return self.context
Заранее спасибо за понимание.