gpt4 book ai didi

Django 表单编码

转载 作者:行者123 更新时间:2023-12-04 18:29:21 24 4
gpt4 key购买 nike

我正在使用表单将数据发送到我的 View 。当我发送 char ó 时,它在 View 中打印为 char �。知道如何正确编码吗?

View .py:

# -*- coding: utf-8 -*-
def myView(request):
if request.method == 'POST':
form = MyForm(request.POST)

if form.is_valid():
title = form.cleaned_data.get('title')
print(title) # prints � instead of ó

表单.py:

# -*- coding: utf-8 -*-
class MyForm(forms.ModelForm):
title = forms.CharField(required=False)

我的表格.html:

<form action="/results/" method="post" content='text/html; charset=UTF-8'>>{% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.title.errors }}
<label for="id_name">Title</label>
{{ form.title }}
</div>

<button type="submit">
<i class="fa fa-thumb-tack"></i> Search
</button>

</form>

最佳答案

我能够通过将此添加到 html 表单来解决问题:

accept-charset="UTF-8"

正确的形式是这样的:

<form action="/results/" method="post" content='text/html; charset=UTF-8' accept-charset="UTF-8"> {% csrf_token %}
{{ form.non_field_errors }}
<div class="fieldWrapper">
{{ form.title.errors }}
<label for="id_name">Title</label>
{{ form.title }}
</div>

<button type="submit">
<i class="fa fa-thumb-tack"></i> Search
</button>

</form>

关于Django 表单编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52808124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com