我正在尝试使用i18n
创建一个多语言网站。
我几乎翻译了所有文字,但我什至无法想象如何翻译我的文章。我在我的项目中使用mixins。
HTML帖子详细信息模板:
<h3>
{% trans post.title %}
</h3>
<h6 style="float: right; opacity: 0.5; margin-top: 6px; margin-right: 30px;">
{{ post.date_pub }}
</h6>
<div style="clear: right;"></div>
<p>
{{ post.body|safe }}
</p>
models.py:
class Post(models.Model):
title = models.CharField(max_length=150, db_index=True)
slug = models.SlugField(max_length=150, blank=True, unique=True)
body = RichTextUploadingField(blank=True, db_index=True)
description = models.TextField(max_length=100, blank=True, db_index=True)
tags = models.ManyToManyField('tag', blank=True, related_name='posts')
date_pub = models.DateTimeField(auto_now_add=True)
def get_absolute_url(self):
return reverse('post_detail_url', kwargs={'slug': self.slug})
def get_update_url(self):
return reverse('post_update_url', kwargs={'slug': self.slug})
def get_delete_url(self):
return reverse('post_delete_url', kwargs={'slug': self.slug})
def save (self, *args, **kwargs):
if not self.id:
self.slug = gen_slug(self.title)
super().save(*args, **kwargs)
def __str__(self):
return self.title
class Meta:
ordering = ['-date_pub']
forms.py
class PostForm(forms.ModelForm):
class Meta:
model = Post
fields = ['title', 'slug', 'body', 'tags', 'description']
widgets = {
'title': forms.TextInput(attrs={'class': 'form-control'}),
'slug': forms.TextInput(attrs={'class': 'form-control'}),
'body': forms.TextInput(attrs={'class': 'form-control'}),
'description': forms.TextInput(attrs={'class': 'form-control'}),
'tags': forms.SelectMultiple(attrs={'class': 'form-control'}),
}
def clean_slug(self):
new_slug = self.cleaned_data['slug'].lower()
if new_slug == 'create':
raise ValidationError('Slug may not be "Create"')
return new_slug
views.py
class PostDetail(ObjectDetailMixin, View):
model = Post
template = 'main/post_detail.html'
class PostCreate(LoginRequiredMixin ,ObjectCreateMixin, View):
form_model = PostForm
template = 'main/post_create_form.html'
raise_exception = True
class PostUpdate(LoginRequiredMixin, ObjectUpdateMixin, View):
model = Post
form_model = PostForm
template = 'main/post_update_form.html'
raise_exception = True
class PostDelete(LoginRequiredMixin, ObjectDeleteMixin, View):
model = Post
template = 'main/post_delete_form.html'
redirect_url = 'posts_list_url'
raise_exception = True
utils.py
class ObjectDetailMixin:
model = None
template = None
def get(self, request, slug):
obj = get_object_or_404(self.model, slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj, 'admin_object': obj, 'detail': True})
class ObjectCreateMixin:
form_model = None
template = None
def get(self, request):
form = self.form_model()
return render(request, self.template, context={'form': form})
def post(self, request):
bound_form = self.form_model(request.POST)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, self.template, 'main/tag_create.html', context={'form': bound_form })
class ObjectUpdateMixin:
model = None
form_model = None
def get(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
bound_form = self.form_model(instance=obj)
return render(request, self.template, context={'form': bound_form, self.model.__name__.lower() :obj})
def post(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
bound_form = self.form_model(request.POST, instance=obj)
if bound_form.is_valid():
new_obj = bound_form.save()
return redirect(new_obj)
return render(request, self.template, context={'form': bound_form, self.model.__name__.lower(): obj})
class ObjectDeleteMixin:
model = None
template = None
redirect_url = None
def get(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
return render(request, self.template, context={self.model.__name__.lower(): obj})
def post(self, request, slug):
obj = self.model.objects.get(slug__iexact=slug)
obj.delete()
return redirect(reverse(self.redirect_url))
答案 0 :(得分:0)
有一个名为django-modeltranslation的软件包,用于在数据库中提供特定于语言的镜像字段,以允许进行特定于内容的翻译。它也有很好的文档-https://django-modeltranslation.readthedocs.io/en/latest/index.html。 这就是该网站Language switch example
所使用的内容