커뮤니티
포인트
쿠폰
내 강의실
국비 신청 내역
증명서
계정
로그아웃
학습 질문
개발 일지
나의 활동
답변 완료
Background on this error at: https://sqlalche.me/e/20/gkpj - DB에 저장이 안되는것 같아요
웹개발이 처음이어도 쉽게 배우는 GPT 웹개발
5주차
북마크
이*관
댓글
1
추천
0
조회수
3
조회수
3
답변 완료

* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.

* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.



최애음악 화면에서 항목을 입력하고,

서브밋 버틀을 누르면 오류가 발생합니다.


제대로 저장이 되지 않는 것 같습니다.

DB애도 입력되지 않네요.


sqlalchemy.exc.IntegrityError

sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: song.username

[SQL: INSERT INTO song (username, artist, title, image_url) VALUES (?, ?, ?, ?)]

[parameters: (None, 'Bruno Mars', '슈퍼샤이', None)]

(Background on this error at: https://sqlalche.me/e/20/gkpj)



작성한 코드 및 에러 메세지

# 필수 라이브러리

'''

0. Flask : 웹서버를 시작할 수 있는 기능. app이라는 이름으로 플라스크를 시작한다

1. render_template : html파일을 가져와서 보여준다

'''

from flask_sqlalchemy import SQLAlchemy

import os

from flask import Flask, render_template, request

app = Flask(__name__)

# DB기본코드


basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] =\

    'sqlite:///' + os.path.join(basedir, 'database.db')


db = SQLAlchemy(app)



class Song(db.Model):

    id = db.Column(db.Integer, primary_key=True)

    username = db.Column(db.String(100), nullable=True)

    artist = db.Column(db.String(100), nullable=False)

    title = db.Column(db.String(100), nullable=False)

    image_url = db.Column(db.String(10000), nullable=False)


    def __repr__(self):

        return f'{self.title} {self.artist} 추천 by {self.username}'



with app.app_context():

    db.create_all()



@app.route("/")

def home():

    name = '최지웅'

    motto = "행복해서 웃는게 아니라 웃어서 행복합니다."


    context = {

        "name": name,

        "motto": motto,

    }

    return render_template('motto.html', data=context)



@app.route("/music/")

def music():

    song_list = Song.query.all()

    return render_template('music.html', data=song_list)



@app.route("/iloveyou/<name>/")

def iloveyou(name):

    motto = f"{name}야 난 너뿐이야!"


    context = {

        'name': name,

        'motto': motto,

    }

    return render_template('motto.html', data=context)



@app.route("/music/create/")

def music_create():


    # form에서 보낸 데이터 받아오기


    username_receive = request.args.get("username")

    title_receive = request.args.get("title")

    artist_receive = request.args.get("artist")

    image_receive = request.args.get("image_url")


# 데이터를 DB에 저장하기

    song = Song(username=username_receive, title=title_receive,

                artist=artist_receive, image_url=image_receive)

    db.session.add(song)

    db.session.commit()

    return render_template('music.html')


if __name__ == "__main__":

    app.run(debug=True)




취소
 공유
취소
댓글 0
댓글 알림
나의얼굴