Skip to content

Instantly share code, notes, and snippets.

@LowerDeez
Created September 8, 2017 09:35
Show Gist options
  • Save LowerDeez/285841195ae7da42fe00abcca3e22a2f to your computer and use it in GitHub Desktop.
Save LowerDeez/285841195ae7da42fe00abcca3e22a2f to your computer and use it in GitHub Desktop.
Django. How to use Django Extra Views and django-parler for Create and Update View with inline_formset
class ArticleForm(TranslatableModelForm):
title = TranslatedField()
short_description = TranslatedField(
label=_('Short description'),
required=False,
help_text=_("You can use html tags for short description. Max length: 1000 characters."))
content = TranslatedField(
label=_('Content'),
form_class=forms.CharField,
widget=TinyMCE,
required=True)
categories = TreeNodeMultipleChoiceField(
label=_("Category"),
queryset=Category.objects.select_related('parent').prefetch_related('translations').all(),
required=False,
help_text=_('You can select few categories')
#level_indicator=mark_safe("    "),
)
class Meta:
model = Article
fields = ['title',
'categories',
'short_description',
'content',
'status']
class ArticleImageForm(forms.ModelForm):
class Meta:
model = ArticleImage
fields = ['image']
labels = {
'image': 'Choose new Image'
}
{% extends 'jinja2/base.jinja.html' %}
{% block title %}
{% if action == 'update' %}
{% trans %}Update article{% endtrans %}
{% else %}
{% trans %}Create new article{% endtrans %}
{% endif %}
{% endblock %}
{% block head %}
<link rel="stylesheet" href="{{ static('articles/css/article.css') }}">
{{ form.media }}
{% endblock %}
{% block breadcrumb %}
<li class="active">
{% if article_action == 'update' %}
{% trans %}Update article{% endtrans %}
{% else %}
{% trans %}Create new article{% endtrans %}
{% endif %}
</li>
{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h2>
{% if article_action == 'update' %}
{% trans %}Update article{% endtrans %}
{% else %}
{% trans %}Create new article{% endtrans %}
{% endif %}
</h2>
<form method="post" class="form" name="create-form" novalidate enctype="multipart/form-data">
{%csrf_token%}
{% include 'jinja2/partial/partial_form_fields.jinja.html' %}
{% for inline in inlines %}
{# <div style="display: none">{{ inline }}</div>#}
<table class="table-responsive table">
{{ inline.management_form }}
{% for form in inline.forms %}
{{ form.id }} <!-------------------------------------IMPORTANT--------------------------------->
{% for field in form.hidden_fields() %}
{% for error in field.errors %}
<div class="alert alert-danger" role="alert">
<span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
<span class="sr-only">{% trans %}Error:{% endtrans %}</span>
{{ error }}
</div>
{% endfor %}
{% endfor %}
{% if loop.index == 1 %}
<thead>
<tr>
{% for field in form.visible_fields() %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="formset_row">
{% for field in form.visible_fields() %}
<td>
{# Include the hidden fields in the form #}
{% if loop.index == 1 %}
{% for hidden in form.hidden_fields() %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors }}
{{ field.as_widget(attrs={'class':'form-control'}) }}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
{% endfor %}
<div class="form-group">
<button type="submit" class="btn btn-success">
<span class="glyphicon glyphicon-ok"></span> Ok
</button>
<a href="{{ url('articles:home') }}" class="btn btn-default">
{% trans %}Cancel{% endtrans %}
</a>
</div>
</form>
</div>
</div>
{% endblock %}
{% block javascript %}
<script src="{{ static('articles/js/article.js') }}"></script>
<script src="{{ static('articles/js/jquery.formset.js') }}"></script>
<script type="text/javascript">
$('.formset_row').formset({
addText: 'Add new Image',
deleteText: 'Remove',
prefix: 'images',
addCssClass: 'btn btn-primary',
deleteCssClass: 'btn btn-danger'
});
$('.formset_row > td > a:not(.btn)').css('display', 'none');
$('.formset_row > td > input[type=checkbox]').css('display', 'none');
$('.formset_row > td > label').css('display', 'none');
$('.formset_row > td > a').each(function(){
if (this.href !== "javascript:void(0)"){
$(this).after(function(){
return '<img style="height: 250px; width: auto;" src="' + this.href + '"/>'
})
}
})
</script>
{% endblock %}
url(r'^create/$', class_views.ArticleCreateView2.as_view(), name='create'),
url(r'^update/(?P<slug>[-\w]+)/$', class_views.ArticleUpdateView2.as_view(), name='update'),
from extra_views import InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView
from parler.views import (
TranslatableModelFormMixin
)
class ArticleImageInlineFormSet(InlineFormSet):
model = ArticleImage
form_class = ArticleImageForm
extra = 1
can_delete = True
class ArticleCreateView2(TranslatableModelFormMixin, CreateWithInlinesView):
model = Article
form_class = ArticleForm
inlines = [ArticleImageInlineFormSet]
template_name = 'articles/jinja2/create-DEV.jinja.html'
view_url_name = 'articles:create'
def get_success_url(self):
return self.object.get_absolute_url()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['article_action'] = 'create'
return context
class ArticleUpdateView2(TranslatableModelFormMixin, UpdateWithInlinesView):
model = Article
form_class = ArticleForm
inlines = [ArticleImageInlineFormSet]
template_name = 'articles/jinja2/create-DEV.jinja.html'
view_url_name = 'articles:update'
slug_field = 'translations__slug'
def get_success_url(self):
return self.object.get_absolute_url()
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['article_action'] = 'update'
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment