0

使用 Flask,我试图根据数据库的内容在同一视图中动态显示各种内容(文本、图片、视频、音频)。文本和图片不会引起任何问题,但是一旦我尝试显示多个媒体(视频或音频),视图就永远不会完全加载。

这些文件不在静态文件夹中。它们使用该send_from_directory()功能受到保护和服务。

py以下是主文件的摘录

# -*- coding: cp1252 -*-
from flask import Flask
from flask.ext.pymongo import PyMongo
from flask import render_template, session, flash, escape, redirect, request, url_for, send_from_directory, Response
from flask.ext.wtf import Form, TextField, TextAreaField, PasswordField, BooleanField, RadioField, SelectField
from flask.ext.wtf import Required, validators, FileField
from flask.ext.login import LoginManager, UserMixin, login_user, logout_user, current_user, login_required
from flaskext.uploads import UploadSet, configure_uploads, IMAGES, AUDIO, UploadNotAllowed
from datetime import datetime
from functools import wraps
import sys, os

app = Flask(__name__)
app.secret_key = 'xxxxxxxxxxxxxxxxxxxxxxx'
mongo = PyMongo(app)

app.jinja_env.globals['mongo'] = mongo

ospath = os.path.dirname(os.path.abspath(__file__))
fullpath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'files')
app.config['UPLOADED_THEFILES_DEST'] = fullpath

login_manager = LoginManager()
login_manager.setup_app(app)

thefiles = UploadSet('thefiles', IMAGES + AUDIO)
configure_uploads(app, (thefiles))

#-----skipping a lot of code 

@app.route('/unlock/<filename>')
def unlock(filename):
    return send_from_directory(app.config['UPLOADED_THEFILES_DEST'], filename)

@app.route('/showdraft')
@login_required
@requires_admin
def showdraft():
    curseur = mongo.db.news.find_one({'num':-1, 'author':session['username']})
    return render_template('showdraft.html', post=curseur)

这是视图:

{% extends "layout.html" %}
{% block body %}
<div class=page>
<style type="text/css">
    pre {
        white-space: pre-wrap;
        font:15px arial,sans-serif;
    }   
    IMG.displayed {
            display: block;
            margin-left: auto;
            margin-right: auto 
    }   
</style>
<h1>{{post.title}}</h1> 

{% for field in post.compo %}
{% for key in field %}

    {% if "Text" in key and field[key]!="" %}
        <p><pre>{{field[key]}}</pre></p>
    {% elif "Image" in key and field[key]!="none" %}
        <p><img class="displayed" src="{{url_for('unlock', filename = field[key])}}" /></p>
    {% elif "Sound" in key and field[key]!="none" %}
        <p><audio  src="{{url_for('unlock', filename = field[key])}}" controls> </audio></p>
    {% elif "Video" in key and field[key]!="none" %}
        <p><video  src="{{url_for('unlock', filename = field[key])}}" width="320" height="240" type="video/ogg" controls></video></p>
    {% else %}
        <p><pre>{{field}}</pre></p>         
        <p><pre>{{field[key]}}</pre></p>
    {% endif %}
{% endfor %}
{% endfor %}

<br><br>By {{post.author}} - {{post.date}}<br>
<a href="{{ url_for('login') }}">Go back to homepage.</a>
</div>
{% endblock %}  

最后是数据库示例(mongodb)

{
  "author": "Myself",
  "compo": [
{
  "Image1": "none"
},
{
  "Text1": "First text !"
},
{
  "Image2": "APicture.jpg"
},
{
  "Text2": "Second text."
},
{
  "Image3": "SomePicture.jpg"
},
{
  "Video1": "SomeVideo.ogv"
},
{
  "Sound1": "SomeSong.ogg"
},
{
  "Sound2": "SomeOtherSong.ogg"
},
{
  "Link1": ""
}
  ],
  "date": "10\/01\/12 17:54:12",
  "num": -1,
  "title": "The Title"
}  

我做错了什么阻止视图加载?

4

0 回答 0