gpt4 book ai didi

python - “ascii”编解码器无法对位置 5 : ordinal not in range(128) error 中的字符 u'\u2019' 进行编码

转载 作者:行者123 更新时间:2023-12-01 04:00:25 25 4
gpt4 key购买 nike

我正在使用 feed 解析器为 django 模型创建对象。

在我的 models.py 中,我有以下模型:

class Content(models.Model):
title = models.CharField(max_length=500)
link = models.URLField()
description = models.TextField()
category = models.CharField(max_length=500, blank=True)

我编写了一个函数来实际创建该模型的对象:

def generate_content_from_feed(rss_url):
parsed_feed = feedparser.parse(rss_url)

for item in parsed_feed['items']:
if not Content.objects.filter(link=item['link']).exists():
content = Content.objects.create(
title=item['title'].encode('utf-8'),
link=item['link'],
description=item['description'].encode('utf-8'),
)
if item['category']:
content.category = item['category']
content.save()

对象创建得很好,但是,在我的管理面板中,某些对象无法打开。其他打开得很好,但对于某些,我收到此错误:

'ascii' codec can't encode character u'\u2019' in position 5: ordinal not in range(128)

我认为这与标题或描述有关,这就是我添加encode('utf-8')的原因,但即使如此,问题仍然存在。我该如何解决这个问题?这里到底有什么问题?

这是我的完整回溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/admin/myapp/content/40/change/

Django Version: 1.9.5
Python Version: 2.7.10
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'myapp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'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']


Template error:
In template /Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/contrib/admin/templates/admin/change_form.html, error at line 21
ascii 11 : {% block coltype %}colM{% endblock %}
12 :
13 : {% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} change-form{% endblock %}
14 :
15 : {% if not is_popup %}
16 : {% block breadcrumbs %}
17 : <div class="breadcrumbs">
18 : <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
19 : &rsaquo; <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
20 : &rsaquo; {% if has_change_permission %}<a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>{% else %}{{ opts.verbose_name_plural|capfirst }}{% endif %}
21 : &rsaquo; {% if add %}{% trans 'Add' %} {{ opts.verbose_name }}{% else %} {{ original|truncatewords:"18" }} {% endif %}
22 : </div>
23 : {% endblock %}
24 : {% endif %}
25 :
26 : {% block content %}<div id="content-main">
27 : {% block object-tools %}
28 : {% if change %}{% if not is_popup %}
29 : <ul class="object-tools">
30 : {% block object-tools-items %}
31 : <li>


Traceback:

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
174. response = self.process_exception_by_middleware(e, request)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
172. response = response.render()

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/response.py" in render
160. self.content = self.rendered_content

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/response.py" in rendered_content
137. content = template.render(context, self._request)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/backends/django.py" in render
95. return self.template.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
206. return self._render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
173. return compiled_parent._render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in _render
197. return self.nodelist.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/defaulttags.py" in render
326. return nodelist.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/loader_tags.py" in render
69. result = block.nodelist.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/defaulttags.py" in render
326. return nodelist.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
992. bit = node.render_annotated(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render_annotated
959. return self.render(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in render
1043. output = self.filter_expression.resolve(context)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/base.py" in resolve
736. new_obj = func(obj, *arg_vals)

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/template/defaultfilters.py" in _dec
47. args[0] = force_text(args[0])

File "/Users/user/my_django_project/myvenv/lib/python2.7/site-packages/django/utils/encoding.py" in force_text
80. s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /admin/myapp/content/40/change/
Exception Value: 'ascii' codec can't encode character u'\u2019' in position 5: ordinal not in range(128)

最佳答案

该异常表明管理模板尝试将您的对象转换为字符串时引发错误:

{{ original|truncatewords:"18" }}

(来自管理模板的第 21 行)。 original 是正在编辑的对象。

您使用的是 Python 2,因此请确保您的模型实现了正确的 __unicode__ 方法,返回 unicode 对象(因此编码)。如果您有一个 __str__ 方法,但它仍然返回 unicode,您肯定会遇到上述异常; __str__ 只能在 Python 3 代码中使用。或者,添加 python_2_unicode_compatible() decorator并使用 __str__,请参阅 __str__ method documentation .

关于python - “ascii”编解码器无法对位置 5 : ordinal not in range(128) error 中的字符 u'\u2019' 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36662239/

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