我在Django-admin
面临编码问题。我有一个模型,它代表一个语言测验,包括内联到Quiz
模型的问题和答案(都是模型)。
我创建了一个简单的csv
导入。在Quiz
模型中,有一个文件字段。当管理员创建新的Quiz
时,他们会上传一个csv文件,该文件调用解析csv的信号并为此测验创建问题和答案。
英语没有问题,但是我试图导入法语测验,这会引起错误。
Exception Value:
'ascii' codec can't encode character u'\ufeff' in position 0: ordinal not in range(128)
导入信号非常简单:
def parse_csv(csv):
questions = []
for line in csv:
line = line.strip('\r\n').strip('\n')
d = {}
items = line.split(';;')
def parse_csv(csv):
questions = []
i=0
for line in csv:
i+=1
print i
line = line.strip('\r\n').strip('\n')
d = {}
items = line.split(';;')
question = items[0]
d['question'] = question
for item in (x.strip() for x in items[1:]):
if '|' in item:
d['answer'] = item
else:
if 'choices' in d.keys():
d['choices'].append(item)
else:
d['choices'] = [item]
if 'answer' not in d.keys():
continue
questions.append(d)
return questions
@receiver(post_save,sender=LanguageQuiz)
def quiz_import_csv(sender,instance,created,**kwargs):
if created:
if instance.import_csv:
questions = parse_csv(instance.import_csv.readlines())
for q in questions:
question = Question(text=q['question'])
question.save()
for ch in q['choices']:
choice = Choice(text=ch,correct=False)
choice.save()
question.choices.add(choice)
answer = Choice(text=q['answer'].strip('|'),correct=True)
answer.save()
question.choices.add(answer)
question.save()
instance.questions.add(question)
instance.save()
你知道我该怎么做吗?
编辑:
TRACEBACK:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/language_tests/languagequiz/7/
Django Version: 1.8.12
Python Version: 2.7.10
Installed Applications:
('django.contrib.auth',
'SolutionsForLanguagesApp',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'super_inlines',
'django_tables2',
'language_tests',
'smart_selects',
'django_extensions',
'constance',
'constance.backends.database',
'nested_inline')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.locale.LocaleMiddleware')
Template error:
In template C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\nested_inline\templates\admin\edit_inline\stacked-nested.html, error at line 9
ascii
1 : {% load i18n admin_static %}
2 : <div class="inline-group{% if recursive_formset %} {{ recursive_formset.formset.prefix|default:"Root" }}-nested-inline {% if prev_prefix %} {{ prev_prefix }}-{{ loopCounter }}-nested-inline{% endif %} nested-inline{% endif %}" id="{{ inline_admin_formset.formset.prefix }}-group">
3 : {% with recursive_formset=inline_admin_formset stacked_template='admin/edit_inline/stacked-nested.html' tabular_template='admin/edit_inline/tabular-nested.html'%}
4 : <h2>{{ recursive_formset.opts.verbose_name_plural|title }}</h2>
5 : {{ recursive_formset.formset.management_form }}
6 : {{ recursive_formset.formset.non_form_errors }}
7 :
8 : {% for inline_admin_form in recursive_formset %}<div class="inline-related{% if forloop.last %} empty-form last-related{% endif %}" id="{{ recursive_formset.formset.prefix }}-{% if not forloop.last %}{{ forloop.counter0 }}{% else %}empty{% endif %}">
9 : <h3><b>{{ recursive_formset.opts.verbose_name|title }}:</b> <span class="inline_label">{% if inline_admin_form.original %} {{ inline_admin_form.original }} {% else %}#{{ forloop.counter }}{% endif %}</span>
10 : {% if inline_admin_form.show_url %}<a href="../../../r/{{ inline_admin_form.original_content_type_id }}/{{ inline_admin_form.original.id }}/">{% trans "View on site" %}</a>{% endif %}
11 : {% if recursive_formset.formset.can_delete and inline_admin_form.original %}<span class="delete">{{ inline_admin_form.deletion_field.field }} {{ inline_admin_form.deletion_field.label_tag }}</span>{% endif %}
12 : </h3>
13 : {% if inline_admin_form.form.non_field_errors %}{{ inline_admin_form.form.non_field_errors }}{% endif %}
14 : {% for fieldset in inline_admin_form %}
15 : {% include "admin/includes/fieldset.html" %}
16 : {% endfor %}
17 : {% if inline_admin_form.pk_field %}{{ inline_admin_form.pk_field.field }}{% endif %}
18 : {{ inline_admin_form.fk_field.field }}
19 : {% if inline_admin_form.form.nested_formsets %}
Traceback:
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\core\handlers\base.py" in get_response
164. response = response.render()
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\response.py" in render
158. self.content = self.rendered_content
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\response.py" in rendered_content
135. content = template.render(context, self._request)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\backends\django.py" in render
74. return self.template.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
210. return self._render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
135. return compiled_parent._render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
65. result = block.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
159. return template.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
212. return self._render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
576. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\loader_tags.py" in render
159. return template.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
212. return self._render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in _render
202. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
576. return self.nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
217. nodelist.append(node.render(context))
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\defaulttags.py" in render
329. return nodelist.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\base.py" in render
905. bit = self.render_node(node, context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render_node
79. return node.render(context)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\template\debug.py" in render
92. output = force_text(output)
File "C:\Users\Milano\PycharmProjects\FutileStudio\SolutionsForLanguages_2\venv\lib\site-packages\django\utils\encoding.py" in force_text
94. s = six.text_type(bytes(s), encoding, errors)
Exception Type: UnicodeEncodeError at /admin/language_tests/languagequiz/7/
Exception Value: 'ascii' codec can't encode character u'\ufeff' in position 8: ordinal not in range(128)
答案 0 :(得分:0)
好吧,我不知道你收到错误的地方。请在收到错误的地方张贴一行。
解析国际文本时,这是一个非常常见的错误。它是通过以下两种方法之一解决的:
try:
print (myThing)
except UnicodeEncodeError:
print (myThing).encode('UTF8')
您还可以尝试以下代码courtesy of David Crook:
dataPath = "somepath.csv" # or other type of file
fil = open(dataPath)
txt = fil.readlines()
txt = ''.join(txt)
works = pd.read_csv(StringIO(txt), index_col = 0)
doesntWork = pd.read_csv(dataPath, index_col = 0)
答案 1 :(得分:0)
我也有同样的问题。 当我上传图片时,我看到了这个问题。 我意识到图像名称必须是英文,所以我将图像名称(其中一个是波斯语)更改为一些随机的英文名称。 为了解决这个问题,为图片字段创建验证器,将图片名称更改为随机英文名称或在上传前更改图片名称。
答案 2 :(得分:-3)
以下字母在法语中很常见:'é','è','ç','à'并且可能会破坏您的代码。
你知道我该怎么做吗?
为它复制问题写一个测试。一旦你编写它,你就可以更快地进行实验(使用内存文件);在测试CSV中的所有单元格中添加有问题的字符;确保问题不再发生等等。
并修复它,可能是使用unicode(...)
。例如:
text=unicode(q['question'])