Flask-Web-App-Tutorial icon indicating copy to clipboard operation
Flask-Web-App-Tutorial copied to clipboard

can u sove this error pls

Open cyberguy01 opened this issue 3 years ago • 2 comments

Traceback (most recent call last): File "c:\Users\simpf\Downloads\web dovolepment\main.py", line 4, in app = create_app() File "c:\Users\simpf\Downloads\web dovolepment\website_init_.py", line 7, in create_app from .views import views ImportError: cannot import name 'views' from 'website.views' (c:\Users\simpf\Downloads\web dovolepment\website\views.py)

cyberguy01 avatar Jan 20 '22 07:01 cyberguy01

Make sure you import the views.py file correctly. And can you attach your code?

thirt33n avatar Jan 20 '22 07:01 thirt33n

Also getting this error. I tried deleting all my code and copying it from this tutorial but get the same error.

PS C:\Users\Jasper\Desktop\Invoice database> & C:/Users/Jasper/AppData/Local/Programs/Python/Python310/python.exe "c:/Users/Jasper/Desktop/Invoice database/main.py" Traceback (most recent call last): File "c:\Users\Jasper\Desktop\Invoice database\main.py", line 3, in app = create_app() File "c:\Users\Jasper\Desktop\Invoice database\website_init_.py", line 7, in create_app from .views import views ImportError: cannot import name 'views' from 'website.views' (c:\Users\Jasper\Desktop\Invoice database\website\views.py) PS C:\Users\Jasper\Desktop\Invoice database>

Here is my init.py and views.py below that.

from flask import Flask from flask_sqlalchemy import SQLAlchemy from os import path from flask_login import LoginManager

db = SQLAlchemy() DB_NAME = "database.db"

def create_app(): app = Flask(name) app.config['SECRET_KEY'] = 'hjshjhdjah kjshkjdhjs' app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}' db.init_app(app)

from .views import views
from .auth import auth

app.register_blueprint(views, url_prefix='/')
app.register_blueprint(auth, url_prefix='/')

from .models import User, Note

create_database(app)

login_manager = LoginManager()
login_manager.login_view = 'auth.login'
login_manager.init_app(app)

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id))

return app

def create_database(app): if not path.exists('website/' + DB_NAME): db.create_all(app=app) print('Created Database!')

from flask import Blueprint, render_template, request, flash, jsonify from flask_login import login_required, current_user from .models import Note from . import db import json

views = Blueprint('views', name)

@views.route('/', methods=['GET', 'POST']) @login_required def home(): if request.method == 'POST': note = request.form.get('note')

    if len(note) < 1:
        flash('Note is too short!', category='error')
    else:
        new_note = Note(data=note, user_id=current_user.id)
        db.session.add(new_note)
        db.session.commit()
        flash('Note added!', category='success')

return render_template("home.html", user=current_user)

@views.route('/delete-note', methods=['POST']) def delete_note(): note = json.loads(request.data) noteId = note['noteId'] note = Note.query.get(noteId) if note: if note.user_id == current_user.id: db.session.delete(note) db.session.commit()

return jsonify({})

Jaspie88 avatar May 14 '22 02:05 Jaspie88