本文介绍了Django管理员。 UnicodeEncodeError'ascii'编解码器无法在位置0-3处编码字符:序数不在范围内(128)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过输入一些非英语字符(其他区域性语言)的表单,具有如下的views.py和models.py并保存到数据库中。提交后,如果我尝试使用管理界面进行编辑。它给出以下错误:

I have views.py and models.py as follows, through the forms I have entered some non-English characters(Other Regional Language), and saved to the database. After submitting if I try to edit with my admin interface. It's giving the following error:

views.py:

def new_submit(request):
    if request.method == 'POST':
        form = NewsForm(request.POST)
        if form.is_valid():
            c = form.save(commit=False)
            c.author = request.user
            c.save()
            return redirect(my_submitted_news)

    else:
        form = NewsForm()
    return render(request, 'new_submit.html', {'form': form})

models.py:

models.py:

class News_Post(models.Model):
    Country = models.CharField(max_length=20)
    State = models.CharField(max_length=20)
    District = models.CharField(max_length=20)
    Area = models.CharField(max_length=20)
    Photo_link = models.CharField(max_length=50, blank=True)
    News_Title = models.CharField(max_length=200)
    News = models.TextField()
    created_date = models.DateTimeField(auto_now_add=True)
    author = models.CharField(max_length=20)

    def __str__(self):
        return self.News_Title

这是回溯:

Environment:


Request Method: GET
Request URL: http://192.168.145.155:8080/anuj_borah/news/news_post/11/change/

Django Version: 1.10.5
Python Version: 2.7.6


Template error:
    In template /usr/local/lib/python2.7/dist-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 %}{% blocktrans with name=opts.verbose_name %}Add {{ name }}{% endblocktrans %}{% 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 "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _legacy_get_response
  249.             response = self._get_response(request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  217.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in _get_response
  215.                 response = response.render()

File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render
  109.             self.content = self.rendered_content

File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in rendered_content
  86.         content = template.render(context, self._request)

File "/usr/local/lib/python2.7/dist-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  208.                     return self._render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  174.         return compiled_parent._render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  174.         return compiled_parent._render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
  315.                 return nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/loader_tags.py" in render
  70.                 result = block.nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/defaulttags.py" in render
  315.                 return nodelist.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in render
  1044.             output = self.filter_expression.resolve(context)

File "/usr/local/lib/python2.7/dist-packages/django/template/base.py" in resolve
  739.                 new_obj = func(obj, *arg_vals)

File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py" in _dec
  47.             args[0] = force_text(args[0])

File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py" in force_text
  80.                 s = six.text_type(bytes(s), encoding, errors)

Exception Type: UnicodeEncodeError at /anuj_borah/news/news_post/11/change/
Exception Value: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)


推荐答案

您还可以在views.py,forms.py和models.py顶部声明:

You could also declare at the top of views.py, forms.py and models.py:

    # -*- coding: utf-8 -*-

这样,您可以声明文件中使用的编码。

This way you declare the encoding used in the file.https://www.python.org/dev/peps/pep-0263/

这篇关于Django管理员。 UnicodeEncodeError'ascii'编解码器无法在位置0-3处编码字符:序数不在范围内(128)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:01