# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=255)
body = models.TextField()
from modeltranslation.translator import translator, TranslationOptions
from articles.models import Article
class ArticleTranslationOptions(TranslationOptions):
fields = ('title', 'body',)
translator.register(Article, ArticleTranslationOptions)
from django import forms
from articles.models import Article
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = ('title', 'title_en', 'title_de', 'body', 'body_en', 'body_de',)
def __init__(self, *args, **kwargs):
super(ArticleForm, self).__init__(*args, **kwargs)
print self.fields
self.fields['title'].widget.attrs = {
'class': 'form-control',
'id': 'title',
}
self.fields['title_en'].widget.attrs = {
'class': 'form-control',
'id': 'title_en',
}
self.fields['title_de'].widget.attrs = {
'class': 'form-control',
'id': 'title_de',
}
self.fields['body'].widget.attrs = {
'class': 'form-control',
'id': 'opt_head',
}
self.fields['body_en'].widget.attrs = {
'class': 'form-control',
'id': 'body_en',
}
self.fields['body_de'].widget.attrs = {
'class': 'form-control',
'id': 'body_de',
}
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.template.response import TemplateResponse
from articles.forms import ArticleForm
from articles.models import Article
def index(request):
article = Article.objects.first()
form = ArticleForm(request.POST or None, instance=article)
if request.method == 'POST':
if form.is_valid():
form.save()
return TemplateResponse(request, 'index.html', {'form': form})
class ArticleForm(TranslationModelForm):
# заменил на
class ArticleForm(forms.ModelForm):
INSTALLED_APPS = [
'стандартные джанго'
]
INSTALLED_APPS += [
'сторонние/установленные'
]
INSTALLED_APPS += [
'свои, добавленные в проект'
]
def blog_new(request):
form = BlogForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
...
return render(request, 'blog_edit.html', {'form': form})
return render(request, 'blog_edit.html', {'form': form})
# или
context = {}
template = "home.html"
return render(request, template, context)
from django.contrib.auth.forms import AuthenticationForm
{% for book in books %}
<button type="button" class="btn btn-primary" data-href='book.url_to_delete' data-toggle="modal" data-target="#bookDeleteModalBox">Удалить</button>
{% endfor %}
<div id="bookDeleteModalBox" class="modal fade">
<form method='delete'>
<button type="button" class="btn btn-primary">Delete</button>
</form>
</div>
$('#bookDeleteModalBox').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget)
var href = button.data('href')
var modal = $(this)
modal.find('form').attr('action', href)
})