Вопрос

I have these 2 forms which appear on one single page (dashboard.html)

# Forms.py

class Area(Form):
    title = TextField("Title", [validators.Required("Please enter an Area Title")])
    text = TextAreaField("Text (max 50 characters)",[validators.Required("Please enter an Area text"),validators.Length(max=50,message="Area text cannot be more than 50 characters")])

class Message(Form):
    Message_title = TextField("Title", [validators.Required("Please enter a Message Title")])
    Message_Date_and_Time = DateTimeField("Date and Time")

And I'm trying to create 3 views to take care of all cases:

  • when I display dashboard.html when getting to the page initially ([GET])
  • when the user sends the Area form (so he obtains validation or error messages depending on the validation) ([POST])
  • when the user sends the Message form (so he obtains validation or error messages depending on the validation) ([POST])

For that I have the following code:

@app.route('/dashboard.html')
def dashboard():
    return render_template('dashboard.html',form= Area(),M_form = Message())

@app.route('/Area',methods=['POST'])
def Area():
    return render_template('dashboard.html', form= Area(), M_form=Message())

@app.route('/Message',methods=['POST'])
def Area():
    return render_template('dashboard.html', form= Area(), M_form=Message())

But when I try to load any of these 3 views I get a RuntimeError: maximum recursion depth exceeded error and the traceback is

return self.view_functions[rule.endpoint](**req.view_args)  
...., line 24, in dashboard  
return render_template('dashboard.html', form= Area(), M_form = Message())  
...., line 31, in Area  
return render_template('dashboard.html', form= Area(), N_form = Message())  
...., line 31, in Area  
return render_template('dashboard.html', form= Area(), N_form = Message())  

The same error again and again until it reaches its clock limit.

And when I just use the dashboard view (after removing both /Area and /Message views altogether from the code) it works just fine. I get my dashboard.html.

I've been looking around, I can t find any connection between the runtimeerror: maximum recursion and forms or WTforms. It seems to be always in connection with a loop not properly closed but here I have no loop. I can't see where its coming from.

Это было полезно?

Решение

You have same name for the Area form and the view functions (/Area and /Message). You're getting the recursion depth error because inside the view function name Area, you are calling Area() again to generate the form but instead the view function is called back in recursion infinitely.

@app.route('/Area',methods=['POST'])
def Area():
    # Area() is called below and it refers to this view function itself
    # that was just declared above.
    return render_template('dashboard.html', form= Area(), M_form=Message())

Change the names of the view functions to something different and unique.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top