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:
UnicodeEncodeError 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
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:
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
Here is the Traceback:
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 : › <a href="{% url 'admin:app_list' app_label=opts.app_label %}">{{ opts.app_config.verbose_name }}</a>
20 : › {% 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 : › {% 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)
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/
In the __str__
method convert your title to a unicode object, that should fix the problem:
def __str__(self):
return unicode(self.News_Title, 'utf-8')
Also, consider switching to Python 3.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With