TypeError:期望的字符串或Unicode对象,找到很久(Flask app)

时间:2015-02-05 13:44:06

标签: unicode flask

这是我的views.py:

# -*- coding: utf-8 -*-
from flask import Blueprint, request, render_template, flash, g, session, redirect, url_for
from flask.ext.login import login_user, logout_user, current_user, login_required
from app import db
from app.users.models import User
from app.news.models import News, NewsImg
from app.main.forms import LoginForm

mod = Blueprint('main', __name__, url_prefix='/')

@mod.route('/', methods=['GET', 'POST'])
def main():
    form = LoginForm()
    if form.validate_on_submit():
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username, password=password).first()
        if user is None:
            flash('Incorrect login or password', 'error')
            return redirect("/")
        login_user(user)
        return redirect("/")
    news = News.query.order_by(News.putdate.desc()).limit(3)
    return render_template("main/main.html", 
        news= news,
        form = form)

授权(Flask-Login)后记录错误: TypeError:期望的字符串或Unicode对象,找不到

在本地计算机上运行,​​问题仅在服务器上

  

[Fri Feb 06 10:58:01 2015] [错误] [client 81.24.82.162] mod_wsgi   (pid = 59836):处理WSGI脚本时发生异常   ' /home/users/s/sadwolf/domains/e-dag.com/dad.wsgi'。,referer:url [周五   2月06日10:58:01 2015] [错误] [客户端81.24.82.162]回溯(大多数   最近的呼叫最后):,referer:url [Fri Feb 06 10:58:01 2015] [错误]   [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py" ;,   第1836行,电话,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162]返回self.wsgi_app(环境,   start_response),referer:url [Fri Feb 06 10:58:01 2015] [错误]   [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py" ;,   1820行,在wsgi_app,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162] response =   self.make_response(self.handle_exception(e)),referer:url [2006年2月5日星期五   2015年10:58:01] [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py" ;,   第1403行,在handle_exception中,referer:url [Fri Feb 06 10:58:01   2015] [错误] [客户端81.24.82.162]重新加注(exc_type,exc_value,   tb),referer:url [Fri Feb 06 10:58:01 2015] [错误] [客户端   81.24.82.162]文件" /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py",   1817行,在wsgi_app,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162] response =   self.full_dispatch_request(),referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py" ;,   第1477行,在full_dispatch_request中,referer:url [Fri Feb 06 10:58:01   2015] [错误] [客户端81.24.82.162] rv =   self.handle_user_exception(e),referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py" ;,   第1381行,在handle_user_exception中,referer:url [Fri Feb 06 10:58:01   2015] [错误] [客户端81.24.82.162]重新加注(exc_type,exc_value,   tb),referer:url [Fri Feb 06 10:58:01 2015] [错误] [客户端   81.24.82.162]文件" /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py",   第1475行,在full_dispatch_request中,referer:url [Fri Feb 06 10:58:01   2015] [错误] [客户端81.24.82.162] rv = self.dispatch_request(),   referer:url [Fri Feb 06 10:58:01 2015] [错误] [client 81.24.82.162]   文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/flask/app.py" ;,   第1461行,在dispatch_request中,referer:url [Fri Feb 06 10:58:01   2015] [错误] [客户端81.24.82.162]返回   self.view_functionsrule.endpoint,referer:url [星期五   2月06日10:58:01 2015] [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/prj/dad/app/main/views.py" ;,第17行,主要的,   referer:url [Fri Feb 06 10:58:01 2015] [错误] [client 81.24.82.162]   user = User.query.filter_by(username = username,   密码=密码).first(),referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/sqlalchemy/orm/query.py" ;,   line 2367,in first,referer:url [Fri Feb 06 10:58:01 2015] [错误]   [client 81.24.82.162] ret = list(self [0:1]),referer:url [Fri Feb   06 10:58:01 2015] [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/sqlalchemy/orm/query.py" ;,   第2228行, getitem ,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162]返回列表(res),referer:url [星期五   2月06日10:58:01 2015] [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/sqlalchemy/orm/loading.py" ;,   第73行,在实例中,referer:url [Fri Feb 06 10:58:01 2015] [错误]   [client 81.24.82.162] rows = [process [0](row,None)for row in   fetch],referer:url [Fri Feb 06 10:58:01 2015] [错误] [客户端   81.24.82.162] File" /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/sqlalchemy/orm/loading.py",   第455行,在_instance,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [client 81.24.82.162] populate_state(state,dict_,row,   isnew,only_load_props),referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162]文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/sqlalchemy/orm/loading.py" ;,   第305行,在populate_state中,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162] populator(state,dict_,row),   referer:url [Fri Feb 06 10:58:01 2015] [错误] [client 81.24.82.162]   文件   " /home/users/s/sadwolf/virtualenv/dagavtodor/lib/python2.7/site-packages/sqlalchemy/orm/strategies.py" ;,   第169行,在fetch_col,referer:url [Fri Feb 06 10:58:01 2015]   [错误] [客户端81.24.82.162] dict_ [key] = row [col],referer:url   [星期五2月06日10:58:01 2015] [错误] [客户端81.24.82.162] TypeError:   期望的字符串或Unicode对象,long found,referer:url

1 个答案:

答案 0 :(得分:1)

请更换所有型号':

def __repr__(self):
    return self.xxx

def __str__(self):
    return self.xxx

到unicode方法,如:

def __unicode__(self):
    return unicode(self.xxx)

由于您的模型可能会显示long类型或null / None类型,因此__repr____str__不支持这些类型。< / p>

__unicode__()生效。

更新:您的语言区域中的数据可能与您的服务器的类型不同&#39; data。因此,本地计算机上不会出现此错误。