I am trying to read parameters from the URL in a Wagtail model function. However, the "request" parameter is not accepted in my model function. I also tried creating a fake function that doesn't read anything, just returns a constant to test that everything else was working fine. This fake function works properly but, as soon as the "request" parameter is added, the function value is not displayed. The problem is without request object, I can't get URL parameters. What am I missing? Do I have to import something for the request parameter to work in the model's functions? Why can't I use the request object in a models function?
example of the URL would use:
http://localhost/blog/?parameter=someValue
MODELS.PY CONTENT
from wagtail.core.models import Page
from wagtail.core.fields import RichTextField
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels [
FieldPanel('intro', classname="full")
]
def read_parameter(self, request):
my_param = request.GET.get('parameter')
return my_param
HTML TEMPLATE CONTENT
{% block content %}
<h1>My page</h1>
<p>This should be the parameter: {{ page.read_parameter }} </p>
{% endblock content %}
EXPECTED OUTCOME:
My Page
This should be the parameter: someValue
ACTUAL OUTCOME
My Page
This should be the parameter:
MODELS.PY WITH THE FAKE FUNCTION ADDED
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels [
FieldPanel('intro', classname="full")
]
def read_parameter(self, request):
my_param = request.GET.get('parameter')
return my_param
def fake_function(self, request):
# this function works as long as the 'request' parameter is not included
# Once I add the request parameter, it stops working
return "fake_value"
HTML TEMPLATE FOR THE FAKE FUNCTION
{% block content %}
<h1>My page</h1>
<p>This should be the parameter: {{ page.read_parameter }} </p>
<br>
<p>Displaying fake value: {{ page.fake_function }} </p>
{% endblock content %}
OUTPUT WITHOUT REQUEST PARAMETER IN FAKE FUNCTION (fake value is displayed)
My page
This should be the parameter:
Displaying fake value: fake_value
OUTPUT WITH REQUEST PARAMETER IN FAKE FUNCTION (fake value is not displayed)
My page
This should be the parameter:
Displaying fake value:
CodePudding user response:
The request object needs to be specifically sent to any method in the class requiring it.
It's a redundant function though since the query string parameters are already present in the template via the request context variable.
{{ request.GET.some_parameter }}
If you want to process that parameter you can use a template tag to return a value.
{% get_something as some_results %}
{% for result in some_results %}
...
{% endfor %}
With template tag:
@register.simple_tag(takes_context=True)
def get_something(context):
parameter = context.request.GET.get('some_parameter', None)
if parameter:
return get_some_results(parameter)
else:
return None
Alternatively, override the model's get_context()
method and do the work there:
class SomeModel(Page):
...
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
some_parameter = request.GET.get('some_parameter', some_default_val)
context['something'] = do_something(some_parameter)
return context
In the template you can access the data with {{ something }}