在我的models.py文件中,我定义了 StandardPage类,我要向其中添加翻译enter code here
。在该类中,我定义了以下这些内容:
class StandardPage(TranslatablePage,Page):
introduction = models.TextField(
help_text='Text to describe the page',
blank=True)
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
)
video_url = models.URLField(null=True,blank=True)
body = StreamField([
( 'main_content',blocks.ListBlock(BaseStreamBlock() ,label = 'content') )]
, blank=True,
null=True,
verbose_name="Page body" )
content_panels = Page.content_panels + [
FieldPanel('introduction', classname="full"),
StreamFieldPanel('body'),
ImageChooserPanel('image'),
FieldPanel('video_url'), ]
@property
def standard_page(self):
return self.get_parent().specific
def get_context(self, request):
language = request.LANGUAGE_CODE
standard_page = request.site.standard_page
pages = TranslatablePage.objects.live().filter(language__code=language).specific().child_of(standard_page)
context['pages'] = self.standard_page
return context
模板base.html包含:
{% load i18n %}
{% load wagtailtrans_tags wagtailcore_tags wagtailuserbar %}
{% load static %}
<!DOCTYPE html>
<html class="no-js">
<head>
<meta charset="utf-8" />
<title>
{% block title_suffix %}
{% if self.seo_title %}{{ self.seo_title }}{% else %}{{ self.title }}{% endif %}
{% endblock %}
</title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
{# Global stylesheets #}
<link rel="stylesheet" type="text/css" href="{% static 'css/mysite.css' %}">
<link href="{% static 'css/bootstrap.min.css' %}" type="text/css" rel="stylesheet" />
<link href="{% static 'fonts/font-awesome-4.7.0/css/font-awesome.min.css' %}" type="text/css" rel="stylesheet" />
<link href="{% static 'css/streamfields.css' %}" type="text/css" rel="stylesheet" />
{% block extra_css %}
{# Override this in templates to add extra stylesheets #}
{% endblock %}
</head>
<body class="{% block body_class %}{% endblock %}">
{% wagtailuserbar %}
{% get_translations page homepage_fallback=False include_self=False as translations %}
{% for language, page in translations.items %}
<link rel="alternate" href="{{ page.full_url }}" hreflang="{{ language.code }}">
{% endfor %}
{% block content %}
{% endblock %}
{# Global javascript #}
<script type="text/javascript" src="{% static 'js/mysite.js' %}"></script>
<script type="text/javascript" src="{% static 'js/jquery-2.2.3.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/bootstrap.min.js' %}"></script>
{% block extra_js %}
{# Override this in templates to add extra javascript #}
{% endblock %}
</body>
</html>
而我的模板“ standard_page.html”包含:
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block content%}
{% include "base/include/header.html" %}
<div class="container">
<div class="row">
<div class="col-md-7">
{{ page.body }}
</div>
</div>
</div>
{% endblock %}
未在页面上呈现内容。即使我发布了特定页面的所有语言,它仍显示欢迎使用新的Wagtail网站!。但是页面标题以提到的不同语言显示 我是django和wagtail的新手。如何纠正这一点,而不是得到。 有帮助吗?