librelist archives

« back to archive

flash won't show in (try, except)

flash won't show in (try, except)

From:
peg kh
Date:
2014-12-11 @ 23:13
I have a wtform in contact.html and wanna display the data that user enter
in info.html But i think there is problem in (try , except) since couldn't
see the flash comment in both and if I want to open info.html got error
AttributeError: 'NoneType' object has no attribute 'name'

  @app.route('/contact', methods=['GET', 'POST'])
  def contact():
  form = LoginForm()
     if request.method == 'POST':
      if form.validate()== True:
      newcontact = Contacts(name=form.name,
                                 email=form.email,
                                subject=form.subject,
                                message=form.message)
    try:
        db.session.add(newcontact)
        db.session.commit()
        flash('Contact commited to database') # It doesn't show the flash???
        # don't know why but it doesn't direct to info when form is valid ???
         return redirect(flask.url_for('info'))
   except:
        #Something went wrong when trying to add to the database.
        flash('Could not commit new contact') # It doesn't show the flash??
    else:
     #If the form does not have all fields that are required
        flash('All fields are required.') # this flash works
 return render_template('contact.html', form=form)
@app.route('/info', methods=['GET', 'POST'])def info():
  form = LoginForm()
  #fetch the first
  contactinfo = Contacts.query.first()
  #Populate the form Not sure about this part??
  #get error AttributeError: 'NoneType' object has no attribute 'name'

  form.name = contactinfo.name
  form.email = contactinfo.email
  form.subject = contactinfo.subject
  form.message = contactinfo.message
  #returns the html page, along with the form
  return render_template('info.html', form=form)

 if __name__ == '__main__':
 app.run(debug=True)

 form.py

 class LoginForm(Form):
     name = StringField("Name", [validators.Required("Please enter
your name.")])
     email = StringField("Email",
     [validators.Required("Please enter your email  address."),
     validators.Email("Please enter valid email address.")])
     subject = StringField("Subject",
     [validators.Required("Please enter your subject.")])
     message = StringField("Message",
     [validators.Required("Please enter your  message.")])
     submit = SubmitField("Submit")

 model.py

     class Contacts(db.Model):
     __tablename__ = "Contacts"
     id = db.Column(db.Integer, primary_key = True)
     name = db.Column(db.String(50))
     email = db.Column(db.String(50))
     subject = db.Column(db.String(50))
     message = db.Column(db.String(50))

 info.html

     {% extends "layout.html" %}
     {% block content %}
       <h2>show the info</h2>
      {% for entry in form %}
         name: {{ entry.name}} <br>
         email: {{ entry.email }} <br>
         subject{{ entry.subject }} <br>
         messaget {{ entry.message }} <br>
        <br>
        {% endfor %}
        {% endblock %}

Re: [flask] flash won't show in (try, except)

From:
Jeremy Dagorn
Date:
2014-12-11 @ 23:45
I think the easiest would be to start debugging in your view.
You can do so by using pdb (import pdb; pdb.set_trace()). That will stop
the execution of your app and you
will be able to inspect the values that you use for your form and so on.
This page will give you a good overview
https://docs.python.org/2/library/pdb.html

I would say:
- verify that your get back an object after your db query as a first step.
- step by step debugging will help you understand what is going on.

Also, try to paste only the relevant code to your issue. The html template
is not needed.

On Thu, Dec 11, 2014 at 3:13 PM, peg kh <peg1988.kh@gmail.com> wrote:

>  I have a wtform in contact.html and wanna display the data that user
> enter in info.html But i think there is problem in (try , except) since
> couldn't see the flash comment in both and if I want to open info.html got
> error AttributeError: 'NoneType' object has no attribute 'name'
>
>   @app.route('/contact', methods=['GET', 'POST'])
>   def contact():
>   form = LoginForm()
>      if request.method == 'POST':
>       if form.validate()== True:
>       newcontact = Contacts(name=form.name,
>                                  email=form.email,
>                                 subject=form.subject,
>                                 message=form.message)
>     try:
>         db.session.add(newcontact)
>         db.session.commit()
>         flash('Contact commited to database') # It doesn't show the flash???
>         # don't know why but it doesn't direct to info when form is valid ???
>          return redirect(flask.url_for('info'))
>    except:
>         #Something went wrong when trying to add to the database.
>         flash('Could not commit new contact') # It doesn't show the flash??
>     else:
>      #If the form does not have all fields that are required
>         flash('All fields are required.') # this flash works
>  return render_template('contact.html', form=form)
> @app.route('/info', methods=['GET', 'POST'])def info():
>   form = LoginForm()
>   #fetch the first
>   contactinfo = Contacts.query.first()
>   #Populate the form Not sure about this part??
>   #get error AttributeError: 'NoneType' object has no attribute 'name'
>
>   form.name = contactinfo.name
>   form.email = contactinfo.email
>   form.subject = contactinfo.subject
>   form.message = contactinfo.message
>   #returns the html page, along with the form
>   return render_template('info.html', form=form)
>
>  if __name__ == '__main__':
>  app.run(debug=True)
>
>  form.py
>
>  class LoginForm(Form):
>      name = StringField("Name", [validators.Required("Please enter your 
name.")])
>      email = StringField("Email",
>      [validators.Required("Please enter your email  address."),
>      validators.Email("Please enter valid email address.")])
>      subject = StringField("Subject",
>      [validators.Required("Please enter your subject.")])
>      message = StringField("Message",
>      [validators.Required("Please enter your  message.")])
>      submit = SubmitField("Submit")
>
>  model.py
>
>      class Contacts(db.Model):
>      __tablename__ = "Contacts"
>      id = db.Column(db.Integer, primary_key = True)
>      name = db.Column(db.String(50))
>      email = db.Column(db.String(50))
>      subject = db.Column(db.String(50))
>      message = db.Column(db.String(50))
>
>  info.html
>
>      {% extends "layout.html" %}
>      {% block content %}
>        <h2>show the info</h2>
>       {% for entry in form %}
>          name: {{ entry.name}} <br>
>          email: {{ entry.email }} <br>
>          subject{{ entry.subject }} <br>
>          messaget {{ entry.message }} <br>
>         <br>
>         {% endfor %}
>         {% endblock %}
>
>


-- 
Jérémy Dagorn
www.jeremydagorn.com

Android apps I developed:
https://play.google.com/store/apps/developer?id=JumyApps