점프 투 플라스크 회원가입 기능 추가하기 질문

models.py

from pybo import db

class Question(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    subject = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text(), nullable=False)
    create_date = db.Column(db.DateTime(), nullable=False)


class Answer(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    question_id = db.Column(db. Integer,
                            db.ForeignKey('question.id', ondelete='CASCADE'))
    question = db.relationship('Question', backref=db.backref('answer_set',))
    content = db.Column(db.Text(), nullable=False)
    create_date = db.Column(db.DateTime(), nullable=False)

class User(db.Model):
    id= db.Column(db.Integer, primary_key=True)
    username= db.Column(db.String(150), unique=True, nullable=False)
    password= db.Column(db.String(200), nullable=False)
    email=db.Column(db.String(120),unique=True, nullable=False)

forms.py

from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, PasswordField
from wtforms.fields.html5 import EmailField
from wtforms.validators import DataRequired, Length, EqualTo, Email

class QuestionForm(FlaskForm):
    subject = StringField('제목', validators=[
        DataRequired('제목은 필수 입력 항목입니다.')])
    content = TextAreaField('내용', validators=[
        DataRequired('내용은 필수 입력 항목입니다.')])

class AnswerForm(FlaskForm):
    content = TextAreaField('내용', validators=[
        DataRequired('내용은 필수입력 항목입니다.')])

class UserCreateForm(FlaskForm):
    username=StringField('사용자이름', validators=[
        DataRequired(), Length(min=3, max=25)])
    password1=PasswordField('비밀번호',validators=[
        DataRequired(), EqualTo('password2', '비밀번호가 일치하지 않습니다')])
    password2=PasswordField('비밀번호확인', validators=[DataRequired()])
    email = EmailField('이메일', [DataRequired(), Email()])

auth_views.py

from flask import Blueprint, url_for, render_template, flash, request
from werkzeug.security import generate_password_hash
from werkzeug.utils import redirect

from pybo import db
from pybo.forms import UserCreateForm
from pybo.models import User

bp=Blueprint('auth', __name__, url_prefix='/auth')

@bp.route('/signup/', methods=('GET', 'POST'))
def signup():
    form=UserCreateForm()
    if request.method=='POST' and form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if not user:
            user=User(username=form.username.data,
                      password=generate_password_hash(form.password1.data),
                      email=form.email.data)
            db.session.add(user)
            db.session.commit()
            return redirect(url_for('main.index'))
        else:
            flash('이미 존재하는 사용자입니다.')
    return render_template('auth/signup.html', form=form)

init.py

from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

import config

db=SQLAlchemy()
migrate=Migrate()


def create_app():
    app = Flask(__name__)
    app.config.from_object(config)

    #ORM
    db.init_app(app)
    migrate.init_app(app,db)
    from . import models

    #블루프린트
    from .views import main_views, question_views, answer_views, auth_views
    app.register_blueprint(main_views.bp)
    app.register_blueprint(question_views.bp)
    app.register_blueprint(answer_views.bp)
    app.register_blueprint(auth_views.bp)

    #필터
    from.filter import format_datetime
    app.jinja_env.filters['datetime']=format_datetime


    return app

명령 프롬프트에서 flask run을 하면 자꾸 ModuleNotFoundError: No module named 'wtforms.fields.html' 이라는 오류가 뜹니다. 왜 그럴까요? ㅠㅠㅠㅠㅠㅠ

421

M 2021년 12월 28일 11:22 오전

wtforms가 버전 업그레이드 되면서 바뀐 부분이 있습니다. 자세한 내용은 다음 페이지의 중요공지를 참고하세요. https://wikidocs.net/81057#_2 - 박응용님, 2021년 12월 28일 2:25 오후 추천 , 대댓글
@박응용님 감사합니다! - 님, 2021년 12월 28일 6:10 오후 추천 , 대댓글
목록으로