Issue
I am learning Python Flask and I am working to a blog as personal project. I am using a combination of Flask and Sqlite but I am stuck because it seems that my system (I am using Windows 10) is not able to find the path to the database. This is my code:
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite: ////C:/Users/admin/Desktop/Blog_Project/blog.db'
db = SQLAlchemy(app)
class Blogpost(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(50))
subtitle = db.Column(db.String(50))
author = db.Column(db.String(20))
date_posted = db.Column(db.DateTime)
content = db.Column(db.Text)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/post')
def post():
return render_template('post.html')
@app.route('/contact')
def contact():
return render_template('contact.html')
@app.route('/prova')
def prova():
return render_template('prova.html')
@app.route('/add')
def add():
return render_template('add.html')
@app.route('/addpost', methods=['POST'])
def addpost():
title = request.form['title']
subtitle = request.form['subtitle']
author = request.form["author"]
content = request.form['content']
post = Blogpost(title=title, subtitle=subtitle, author=author, content=content, date_posted=datetime.now())
db.session.add(post)
db.session.commit()
return redirect(url_for('index'))
if __name__ == "__main__":
app.run(debug = True)
But when I try to add a post in the corresponding webpage, I get this error:
sqlalchemy.exc.ArgumentError
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'sqlite: ////C:/Users/admin/Desktop/Blog_Project/blog.db'
Actually the database should exist, since I see the file in my folder (the path is the one in the code)
Have you got any idea how I can solve the problem?
Solution
Try using double slashes:
sqlite: ////C:\\Users\\admin\\Desktop\\Blog_Project\\blog.db
or if you want to stay on windows, use the windows formation:
sqlite: ////C:\Users\admin\Desktop\Blog_Project\blog.db
you can learn more in this detailed answer: Windows path in Python
Answered By - shiny
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.