Wagtail:如何在 models.py 中的页面 class 中访问 'request'
Wagtail: How can I access 'request' in a Page class in models.py
这是home/models.py:
class HomePage(RoutablePageMixin, Page):
template = "home/home_page.html"
components = {'key1': ['alias1', 'ref1'],
'key2': ['alias2', 'ref2'],
'key3': ['alias3', 'ref3'],
'key4': ['alias4', 'ref4'],
'key5': ['alias5', 'ref5']}
def get_comps(self):
comps_list = []
for name in self.components.keys():
alias = self.components.get(name)[0]
current_page_url = request.build_absolute_uri
sub_url_index = current_page_url.find(".")
ref = "http://" + self.components.get(name)[1] + "." + current_page_url[sub_url_index+1:]
the_comp = comp(name, alias, ref)
comps_list.append(the_comp)
return comps_list
class comp(models.Model):
comp_name = ""
comp_alias = ""
comp_ref = ""
def __init__(self, name, alias, ref):
self.comp_name = name
self.comp_alias = alias
self.comp_ref = ref
Wagtail 在以下行显示错误:
current_page_url = request.build_absolute_uri
这是因为找不到正确的请求对象。
如何访问 get_comps() 中的请求对象?我不想用 page.get_full_url 或任何页面方法替换它,因为那不会给我绝对的 URL.
尝试将您的逻辑放在 serve(self, request)
方法中:http://docs.wagtail.io/en/v2.0/topics/pages.html#more-control-over-page-rendering
这是我做的:
def get_comps(self, request):
comps_list = []
for name in self.components.keys():
alias = self.components.get(name)[0]
current_page_url = request.build_absolute_uri()
sub_url_index = current_page_url.find(".")
ref = "http://" + self.components.get(name)[1] + "." + current_page_url[sub_url_index+1:]
the_comp = comp(name, alias, ref)
comps_list.append(the_comp)
return comps_list
def get_context(self, request):
context = super().get_context(request)
comps_list = self.get_comps(request)
context['comps_list'] = comps_list
return context
在模板 (components.html) 中,我有以下内容:
{% for comp in comps_list %}
<div class="row">
<div class="col-md py-2">{{ comp.comp_name }}</div>
<div class="col-md">
<a class="nav-link" href="{{comp.comp_ref}}" target="_blank">{{comp.comp_alias}}</a>
</div>
</div>
{% endfor %}
这是home/models.py:
class HomePage(RoutablePageMixin, Page):
template = "home/home_page.html"
components = {'key1': ['alias1', 'ref1'],
'key2': ['alias2', 'ref2'],
'key3': ['alias3', 'ref3'],
'key4': ['alias4', 'ref4'],
'key5': ['alias5', 'ref5']}
def get_comps(self):
comps_list = []
for name in self.components.keys():
alias = self.components.get(name)[0]
current_page_url = request.build_absolute_uri
sub_url_index = current_page_url.find(".")
ref = "http://" + self.components.get(name)[1] + "." + current_page_url[sub_url_index+1:]
the_comp = comp(name, alias, ref)
comps_list.append(the_comp)
return comps_list
class comp(models.Model):
comp_name = ""
comp_alias = ""
comp_ref = ""
def __init__(self, name, alias, ref):
self.comp_name = name
self.comp_alias = alias
self.comp_ref = ref
Wagtail 在以下行显示错误:
current_page_url = request.build_absolute_uri
这是因为找不到正确的请求对象。
如何访问 get_comps() 中的请求对象?我不想用 page.get_full_url 或任何页面方法替换它,因为那不会给我绝对的 URL.
尝试将您的逻辑放在 serve(self, request)
方法中:http://docs.wagtail.io/en/v2.0/topics/pages.html#more-control-over-page-rendering
这是我做的:
def get_comps(self, request):
comps_list = []
for name in self.components.keys():
alias = self.components.get(name)[0]
current_page_url = request.build_absolute_uri()
sub_url_index = current_page_url.find(".")
ref = "http://" + self.components.get(name)[1] + "." + current_page_url[sub_url_index+1:]
the_comp = comp(name, alias, ref)
comps_list.append(the_comp)
return comps_list
def get_context(self, request):
context = super().get_context(request)
comps_list = self.get_comps(request)
context['comps_list'] = comps_list
return context
在模板 (components.html) 中,我有以下内容:
{% for comp in comps_list %}
<div class="row">
<div class="col-md py-2">{{ comp.comp_name }}</div>
<div class="col-md">
<a class="nav-link" href="{{comp.comp_ref}}" target="_blank">{{comp.comp_alias}}</a>
</div>
</div>
{% endfor %}