Reputation: 11
I am new to programming and Flask. I am trying to create a chatroom type app for which initially I have to create a user Register and login system but I am facing issues with the on submitting the form. The submit button that I am using with Wtforms does absolutely nothing, neither sends any post request to the server. Please assist!
from flask import Flask, render_template, flash, request, redirect, url_for
from wtforms import Form, StringField, TextAreaField, PasswordField
from wtforms.validators import DataRequired, Length, Email, EqualTo
class RegisterForm(Form):
name = StringField('Name', validators=[Length(min=1, max=50)])
username = StringField('Username', validators=[Length(min=3, max=50)])
email = StringField('Email', validators=[Length(min=7, max=50)])
password = PasswordField('Password', validators=[DataRequired(), EqualTo('confirm', message='Password do not match!')])
confirm = PasswordField('Confirm Password')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method =='POST' and form.validate():
name = form.name.data
username = form.username.data
email = form.email.data
password = sha256_crypt.encrypt(str(form.password.data))
#create cursor
cur = mysql.connection.cursor()
#Executing the Query
cur.execute("INSERT INTO users(name, username, email, password) VALUES (%s, %s, %s, %s)", (name, username, email, password))
#commit to DB
mysql.connection.commit()
#close cursor
cur.close()
flash('You have registered to the Chatter!', 'success')
return redirect(url_for('index'))
return render_template('register.html', form=form)
The HTML code:
{% extends 'layout.html' %}
{% block content %}
<h2 style="color: brown;">Register to Join the chatroom!</h2>
<form method="POST" action="{{ url_for('register') }}">
{{ form.csrf_token }}
{{form.name.label}}
{{form.name}}
{{form.username.label}}
{{form.username}}
{{form.password.label}}
{{form.password}}
{{form.confirm.label}}
{{form.confirm}}
</form>
<p><input type="submit", name="submit"></p>
{% endblock %}
Upvotes: 1
Views: 1543
Reputation: 183
Because you don't have a submit button in the form.
Add this to RegisterForm :
submit = SubmitField('Sign Up!')
Add this to the HTML code :
{{form.submit}}
Also don't use SQL like a barbarian. Use Flask-SQLAlchemy. You'll be in heaven.
Upvotes: 1