我在flask_admin视图(仪表板)中没有得到内容?

时间:2019-12-30 02:39:07

标签: python-3.x flask flask-sqlalchemy flask-admin

我正试图在flask_admin仪表板中查看所有表(烧瓶应用程序)的视图,但我没有在flask_admin中获取任何表。我不知道怎么了。一切似乎都正确。我会尝试其他方式,但仍然停留在这里吗?

我没有收到任何错误。我的flask_admin仪表板有问题。

Click to see flask_admin

Model.py:

from flask_admin.contrib.sqla import ModelView
from flask_login import UserMixin
import datetime
from flask_wtf import FlaskForm
from wtforms import StringField, BooleanField, PasswordField
from wtforms.validators import input_required, Length, Email
from app import db,admin

date = datetime.datetime.strftime(datetime.datetime.now(), '%B %d, %Y')


class Contacts(db.Model):
    __tablename__ = 'contacts'
    id = db.Column('No', db.Integer, primary_key=True)
    name = db.Column('Name', db.String(50), nullable=False)
    email = db.Column('Email', db.String(50), nullable=False)
    phone = db.Column('Phone', db.String(15), nullable=False)
    message = db.Column('Message', db.Text, nullable=False)
    date = db.Column('Date', db.VARCHAR(20), default=date)

    def __repr__(self):
        return '<Contacts(Name=%s, Email=%s, Phone=%s, Message=%s, Date=%s)>' % (
            self.name, self.email, self.phone, self.message, self.date)


class Post(db.Model):
    __tablename__ = 'post'
    id = db.Column('No', db.Integer, primary_key=True)
    title = db.Column('Title', db.Text, nullable=False)
    body = db.Column('Body', db.Text, nullable=False)
    tag = db.Column('Tag', db.Text, nullable=False)
    like = db.Column('Like', db.Text, nullable=True)
    url = db.Column('Url', db.Text, nullable=False)
    date = db.Column('Date', db.String(20), nullable=False, default=date)

    def __repr__(self):
        return '<Post(title=%s, body=%s, tag=%s, like=%s, url=%s date=%s)>' % (self.title, self.body, self.tag, self.like, self.url, self.date)


class Login(db.Model, UserMixin):
    __tablename__ = 'login'
    id = db.Column('No', db.Integer, primary_key=True)
    username = db.Column('Username', db.String(50), nullable=False)
    email = db.Column('Email', db.String(100), nullable=False)
    password = db.Column('Password', db.String(50), nullable=False)
    date = db.Column('Date', db.String(20), nullable=False, default=date)


    def get_id(self):
        return self.id
    @property
    def is_authenticated(self):
        return True
    @property
    def is_active(self):
        return True
    @property
    def is_anonymous(self):
        return False
    @property
    def __repr__(self):
        return '<Login(username=%s, email=%s, password=%s, date=%s)>' % (self.name, self.email, self.password, self.date)


# ------------------------------------ flask_admin view -------------------------------------
admin.add_view(ModelView(Post, db.session))
admin.add_view(ModelView(Login, db.session))
admin.add_view(ModelView(Contacts, db.session))

app.py:

from flask import Flask, render_template, url_for, redirect, flash, request, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_required, login_user
from flask_bootstrap import Bootstrap
from werkzeug.utils import secure_filename
import os, datetime
from sqlalchemy import or_
from flask_admin import Admin


date = datetime.datetime.strftime(datetime.datetime.now(), '%B %d, %Y')


app = Flask(__name__, static_folder='static', template_folder='template')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://rohit:password@localhost/flask'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SECRET_KEY'] = 'KeepItSecret,DonotTellAnyone'
app.config['UPLOAD_FOLDER'] = 'static/img/'

ALLOW_EXTENSIONS = {'jpg','png','txt','jpeg','gif','pdf'}


db = SQLAlchemy(app)
login_manager = LoginManager()
bootstrap = Bootstrap(app)
admin = Admin(app)




from model import *

# login init
db.init_app(app)
login_manager.init_app(app)




@login_manager.user_loader
def user_loader(user_id):
    return Login.query.get(user_id)

.........

admin / index.html:

{% extends 'admin/master.html' %}
{% if 'user_name' in session %}
    {{ super() }}
    {% block messages %}
        <h1>Hi, there.</h1>
    {% endblock messages %}
{% endif %}

请帮忙弄清楚!

0 个答案:

没有答案