【问题标题】:Why would This Flask Page not render and show a 500 ERROR?为什么此 Flask 页面不会呈现并显示 500 错误?
【发布时间】:2019-11-02 21:48:02
【问题描述】:

我对 Flask 和 SQLAlchemy 比较陌生,但我们一直在训练营中学习它们。我想我会尝试与他们一起创建一个网站,但遇到了困难。其他页面工作正常,此页面在本地测试机上工作,但弹出 500 错误?我想它可能在 app.config['SQLALCHEMY_DATABASE_URI'] 中,但是很难找到一个好的解释。非常感谢任何帮助!

from flask import Flask, request, redirect, render_template, flash, session, url_for
import cgi
import os
import math
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:password@/31.220.50.227:3306'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
app.secret_key = 'y337kGcys&zP3B'

class Catalog(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    img_location = db.Column(db.String(120))
    pdf_location = db.Column(db.String(120))
    title = db.Column(db.String(120))


    def __init__(self, img_location, pdf_location, title):
        self.img_location = img_location
        self.pdf_location = pdf_location
        self.title = title



@app.route('/')
def index():
    return render_template('index.html')

@app.route('/catalogs')
def catalogs():
    page = request.args.get('page', 1, type=int)

    catalogs = Catalog.query.paginate(page=page,per_page= 5)
    next_url = url_for('catalogs', page=catalogs.next_num) \
        if catalogs.has_next else None
    prev_url = url_for('catalogs', page=catalogs.prev_num) \
        if catalogs.has_prev else None
    img_location = request.args.get("img_location")
    down_location = request.args.get("down_location")
    return render_template('catalogs.html',title="Camera Eccentric", catalogs=catalogs, next_url=next_url, prev_url=prev_url)
{% extends "base.html" %}

{% block content %}
<div class="catalogs">
    <div class="container-fluid">
        <div class="row">
            {% for catalog in catalogs.items %}
            <div class="col-sm text-center">
                <a href="{{catalog.pdf_location}}"><img class="img-fluid" src="{{catalog.img_location}}">
                <h3>{{catalog.title}}</h3></a>
            </div>
            {% endfor %}
        </div>
    </div>
    <hr>
    <div class="container-fluid">
        <div class="row">
                <div class="col-sm text-center">  
                    {% if prev_url %}
                    <a href="{{ prev_url }}"><h3>Previous</h3></a>
                    {% endif %}
                </div>
                <div class="col-sm text-center">
                    {% for page_num in catalogs.iter_pages(left_edge=1, right_edge=1, left_current=1, right_current=2) %}
                    {% if page_num %}
                    {% if catalogs.page == page_num %}
                    <a class="btn btn-custom mb-4" href="{{ url_for('catalogs', page=page_num) }}">{{ page_num }}</a>
                    {% else %}
                    <a class="btn btn-outline-custom mb-4" href="{{ url_for('catalogs', page=page_num) }}">{{ page_num }}</a>
                    {% endif %}
                    {% else %}
                    ...
                    {% endif %}
                    {% endfor %}
                </div>
                <div class="col-sm text-center">
                    {% if next_url %}
                    <a href="{{ next_url }}"><h3>Next</h3></a>
                    {% endif %}
                </div> 
                <div class="row mx-0">
                    <div class="col-12 bg-light text-dark">

                        <p>Researching classic cameras and lenses can be a daunting process.
                            We hope these pages will not only inform photographers about vintage camera equipment,
                            but inspire others to start using them in their future photographic and creative endeavors.
                        </p>
                        <p>We will continue to update this page with photographs, experiences, and catalogs.
                            We hope you find these helpful and greatly appreciate any contributions from other
                            photographers : )
                        </p>
                </div>
        </div>
    </div>
</div>
{% endblock %}

【问题讨论】:

标签: flask-sqlalchemy


【解决方案1】:

我想通了!似乎无论是烧瓶,sqlalchemy 都连接到具有小写名称的表,而我有一个具有大写名称的表。将名称更改为小写,页面正常工作!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多