Reputation: 41
This is 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 displays an error at the line:
current_page_url = request.build_absolute_uri
This is because the request object cannot be found which is correct.
How can I access the request object in get_comps()? I do not want to replace this with page.get_full_url or any page methods since that does not give me the absolute URL.
Upvotes: 1
Views: 1195
Reputation: 41
This is what I did:
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
In the template (components.html), I have the following:
{% 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 %}
Upvotes: 0
Reputation: 3098
Try placing your logic in the serve(self, request)
method: http://docs.wagtail.io/en/v2.0/topics/pages.html#more-control-over-page-rendering
Upvotes: 2