how to get post variable from a form?

698 views
Skip to first unread message

Thierry Durand

unread,
Jul 16, 2015, 3:39:18 PM7/16/15
to wag...@googlegroups.com
Hi,
I'm trying to use wagtail for the first time, i did a form where i can select a list of url

<form method='post' action='{% pageurl self %}' class='seek'>
{% csrf_token %}
<SELECT class='input1' name='url' size='1' onChange='submit()'>
    <option value=''>Choose an url</option>
    {% for url,id in self.urls %}
        <option VALUE='{{id}}' >{{url}}</option>
    {% endfor %}
    </select>
</form>


the form display correctly, I would like now to be able to read the contain of the post variables from my model

how can I do this ??

best regards

Matthew Westcott

unread,
Jul 17, 2015, 6:15:50 AM7/17/15
to wag...@googlegroups.com
Hi Thierry,
You can do this by defining a custom 'serve' method on your Page model <http://docs.wagtail.io/en/v1.0/reference/pages/model_recipes.html#overriding-the-serve-method>. The default behaviour is simply to render the page template, but you can put in any logic that generates a Django response object, just like writing a standard view method in Django. For example, you could do:

from django.http import HttpResponse

class ExamplePage(Page):
# ... define page fields here ...

def serve(self, request):
if request.method == 'POST':
return HttpResponse('<h1>you chose URL %s</h1>' % request.POST['url'])
else:
# display the page as usual
return super(ExamplePage, self).serve(request)


Cheers,
- Matt

Thierry Durand

unread,
Jul 17, 2015, 9:03:14 AM7/17/15
to wag...@googlegroups.com
ok thanks, your script works but it's not what i want to do , i don't to display the page normally OR just display a value.

I want to display my page in any case and be able to read the post value in my page, like in php when you simply read $_POST.

so if the only way to do it is to use this, how can i pass all the post value into my page ??

regards

Matthew Westcott

unread,
Jul 17, 2015, 9:17:41 AM7/17/15
to wag...@googlegroups.com
OK, in that case you can use a 'get_context' method:

class ExamplePage(Page):
# ... define page fields here ...

def get_context(self, request):
if request.method == 'POST':
return {
'self': self,
'chosen_id': request.POST['url'],
}
else:
return {
'self': self,
'chosen_id': None,
}

The posted value is then available in the template as 'chosen_id'; or if the page is being viewed directly without a form post, chosen_id will be None.

Cheers,
- Matt

Thierry Durand

unread,
Jul 17, 2015, 9:27:02 AM7/17/15
to wag...@googlegroups.com
thank for the very quick answer,

it's exactly what I need, I cannot make your example work, {{self.chosen_id}} in my template is always empty ....

Matthew Westcott

unread,
Jul 17, 2015, 9:33:45 AM7/17/15
to wag...@googlegroups.com
You should use {{ chosen_id }}, not {{ self.chosen_id }}. The get_context method adds the new variable alongside 'self' - 'self' is only used to access properties of the page.

- Matt

Thierry Durand

unread,
Jul 17, 2015, 9:33:46 AM7/17/15
to wag...@googlegroups.com
ok, it works, it's not {{self.chose_id}} but just {{chosen_id}}

Thank you very much, I can go on now.

best regards,

Thierry

Thierry Durand

unread,
Jul 18, 2015, 9:04:13 AM7/18/15
to wag...@googlegroups.com
Hi again,

now I know how to read post form data in template, but what if I need to read them directly from the model

indeed, i need to get my id (created by the form) in the model to do a sql request to a database and the return some values to the template, got it?

I don't know how to do that ...


regards,

Thierry

Thierry Durand

unread,
Jul 18, 2015, 1:06:12 PM7/18/15
to wag...@googlegroups.com
ok, it was really a dumb ass question, it's ok, no problem to do it.

Matthew Westcott

unread,
Jul 20, 2015, 5:40:16 AM7/20/15
to wag...@googlegroups.com
No problem! If you're coming to Wagtail from the PHP world, I'd recommend following the Django tutorial: https://docs.djangoproject.com/en/1.8/intro/ . Django does a few things differently from PHP, such as encouraging you to move your application logic out of templates - it also provides tools for object lookups so that you don't have to write your own SQL queries. Everything you learn there can be applied to a Wagtail site - but if you're doing lots of custom data handling, you may find that working directly in Django is a better fit.

Cheers,
- Matt

On 18 Jul 2015, at 18:06, Thierry Durand <weblayo...@gmail.com> wrote:

> ok, it was really a dumb ass question, it's ok, no problem to do it.
>
> --
> You received this message because you are subscribed to the Google Groups "Wagtail support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to wagtail+u...@googlegroups.com.
> To post to this group, send email to wag...@googlegroups.com.
> Visit this group at http://groups.google.com/group/wagtail.
> To view this discussion on the web, visit https://groups.google.com/d/msgid/wagtail/28086d74-cdb5-4ca9-839d-cc9eda8a3376%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages