Трясогузка: как я могу получить доступ к запросу в классе страницы в models.py - PullRequest
0 голосов
/ 06 марта 2020

Это 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.

Ответы [ 2 ]

2 голосов
/ 06 марта 2020

Попробуйте разместить логи c в методе serve(self, request): http://docs.wagtail.io/en/v2.0/topics/pages.html#more -control-over-page-render

0 голосов
/ 07 марта 2020

Вот что я сделал:

   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

В шаблоне (компоненты. 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 %}

...