繁体   English   中英

“utf-8”编码在开发服务器上有效,但在部署时无效

[英]"utf-8" encoding works on development server but not when deployed

我在 pythonanywhere.com 上托管的 Django 应用程序不支持 utf-8 编码。 'é' 之类的字符返回错误。

意见

def result_view(request):
    if request.method == 'POST':
        search = request.POST.get('textfield').encode("utf-8")
        print search

        try:
            try:
                value = wikipedia.page(search)
                title = value.title
                url = value.url
                print title
                data = wikipedia.summary(search, sentences=10)

            except wikipedia.exceptions.DisambiguationError as e:
                data = e
                title = search + " (Disambiguation)"
                u = search.replace(" ", "_")
                url = "https://en.wikipedia.org/wiki/" + u

        except:
            raise Http404()

        return render(request, "search/result.html", {'title': title, 'url': url, 'data': data})

    else:
        return render(request, "search/result.html", {})

textfield输入使用 utf-8 编码,在 Django 开发服务器中工作正常,但在我的pythonanywhere服务器中返回 404 页。

模板

<form name="myform" method="POST" action="{% url 'result' %}">
    <div class="form-group">
        <div class="input-group">
            {% csrf_token %}
            <input type="text" class="form-control" name="textfield" placeholder="Search" required/>
            <div class="input-group-addon">
                <span class="glyphicon glyphicon-search"></span>

            </div>
        </div>
    </div>

    <button type="submit" class="btn btn-danger btn-lg ">Search</button>
</form> 

您必须允许 Pythonanyhere 使用的数据库(从这里开始它应该是 MySQL,但可以更改)使用编码 utf-8,为此,只需启动 MySQL 控制台,然后运行以下命令(将 databasename 替换为您的数据库名称,当然):

 ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ci;

然后,对于每个表运行这个:

 ALTER TABLE tablename CHARACTER SET utf8 COLLATE utf8_general_ci;

utf-8 是用于在数据库中存储非拉丁字符(例如西里尔文)的标准编码,然后您需要更改其字符集和排序规则设置,首先在您的数据库上,然后在每个表上。

如果您有很多表,您可以使用存储过程执行这些命令。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM