Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8689267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:36:10+00:00 2026-06-12T23:36:10+00:00

I have a page template with a view class. In the page template, I

  • 0

I have a page template with a view class. In the page template, I have a button that submits to the same page i.e.

<form method="post" tal:attributes="action request/getURL" >
    <input type="hidden" name="filename" value="" tal:attributes="value python:item['filename']" />
    <input type="submit" name="form.action.convert" value="Convert" /> 
</form>

When the form is submitted, the view class is called.

class Html(BrowserView):

    def __init__(self, context, request): 
        self.request = request
        self.context = context         

    def __call__(self):
        # Is this a form submission via POST?
        req = self.request
        if req.get('REQUEST_METHOD', 'POST') and \
            req.form.get('form.action.convert', '') == 'Convert': 

            self.convert_document(self.context, str(req.form.get('filename', '')))


    def convert_document(self, contextObj, fileToConvert):
       """ Do something """
       return None

Now, the problem is I cannot put the logic in the __init__ method since this method is called multiple times, resulting in multiple form submissions with just a single click. However, the __call__ method is called once when the button is clicked, but unfortunately, if I click a link to view the current content item in the view, nothing happens, since the __call__ method is called and nothing happens.

I cannot use the code below in the __call__ method. The browser will complain that the page is redirecting in a manner that will never end.

self.context.REQUEST.response.redirect( self.context.absolute_url() )

Is there a BETTER way of handling form submissions in a page template in Plone? How do I call my method (i.e. convert_document ) in the view class from the page template?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:36:12+00:00Added an answer on June 12, 2026 at 11:36 pm

    First, some comments on your code, it can be simplified and improved:

    Best practice is for your form to use the absolute_url() method of your view; it’s the canonical URL, while request/getURL might include acquisition oddities or the wrong URL altogether if your view was included elsewhere:

    <form method="post" tal:attributes="action view/absolute_url" >
        <input type="hidden" name="filename" value="" tal:attributes="value item/filename" />
        <input type="submit" name="form.action.convert" value="Convert" /> 
    </form>
    

    The filename input box tal expression can just use a path expression, these work for dictionaries just fine.

    You don’t need to redefine the __init__ method, the BrowserView class already provides that for you.

    To test if a form has been submitted, we generally just test for the submit button being present in the request, your test is more elaborate than is needed:

    def __call__(self):
        if 'form.action.convert' in self.request.form: 
            self.convert_document()
    

    Because convert_document is a method of your view, it can itself access self.context and self.request.form['filename'], simplifying your method signature. Since you now know that your form is submitted, you can count on self.request.form['filename'] to exist and be a string; if it isn’t someone has been manually tinkering with the request and things deserve to break, so I generally do not use get(..., '') in such cases.

    Views must always do all their work in the __call__ method, since the __init__ is called during traversal, at which time such crucial information as the current user have not yet been determined. You do not need to access the response for the redirect method through the context, just access it from the self.request attribute, but remember to return the result:

    return self.request.response.redirect(self.context.absolute_url())
    

    However, if your view is the default view of the context object, that will indeed lead to a redirect loop that the browser will not tolerate.

    Note that if you do override the __call__ method of a BrowserView view class that has a template associated with it, you need to make sure you return the output of the template where appropriate. My stab at your view class would thus be:

    class Html(BrowserView):
        def __call__(self):
            if 'form.action.convert' in self.request:
                self.convert_document()
                return self.request.response.redirect(self.context.absolute_url())
            return self.index()
    
        def convert_document(self):
           """ Do something """
           context = self.context
           filename = self.request.form['filename']
           # Do something with the context and filename
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this as template account_form.html <form action=/contact/ method=post> {% for field in form
I have a basic layout page template: --- layout: default --- <header class="sidebar"> {{
I have a ModelForm that I have created a view and template with to
In my .xhtml page, I have the following form: <ui:composition xmlns:ui=http://java.sun.com/jsf/facelets template=./../template/CustomerTemplate.xhtml xmlns:h=http://java.sun.com/jsf/html xmlns:f=http://java.sun.com/jsf/core
Description I have a payment page that includes a form for entering bank account
I have a Django template that contains a Javascript function, like so: <a class
I have just meticulously followed MS instructions for customising a dynamic data page template
I have an ASP.Net web page, which uses a Master Page template. The page
I am displaying the iframe in wordpress page template. In iframe src i have
I have a one-page OpenOffice document which is a POD template. Basically, I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.