首页 文章

TypeError:强制转换为Unicode:需要很长的字符串或缓冲区

提问于
浏览
-2
class Movie(models.Model):
    movie_id = models.BigAutoField(primary_key=True)
    movie_name = models.CharField(max_length=128)
    movie_lang = models.CharField(max_length=36)
    director = models.CharField(max_length=72)
    producer = models.CharField(max_length=72)
    production_house = models.CharField(max_length=128)
    lead_actor = models.CharField(max_length=72)
    lead_actress = models.CharField(max_length=72)
    music_director = models.CharField(max_length=72)
    art_director = models.CharField(max_length=72)
    stunts = models.CharField(max_length=72)
    cinematography = models.CharField(max_length=72)
    costume_design = models.CharField(max_length=72)
    hair_stylist = models.CharField(max_length=72)  

    def __str__(self):
        return self.movie_name

class Cover(models.Model):
    cover_id = models.BigAutoField(primary_key=True)
    cover_path = models.CharField(max_length=512)
    movie = models.ForeignKey('Movie',on_delete=models.CASCADE) 

    def __str__(self):
        return self.cover_id

    def unicode(self):
        return unicode(self.cover_path)




#urls.py
from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^(?P<question_id>[0-9]+)/$',views.movie,name='movie'),
    url(r'^vedio/',views.vedio,name='vedio'),
    url(r'^image/',views.image,name='image'),
    url(r'^$',views.index,name = 'index'),
]

#views.py

from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, world. You're at the movie station.")

def movie(request,question_id):
    return HttpResponse("Welcome to movies page")

def vedio(request):
    return HttpResponse("Welcome to vedios page")

def image(request):
    return HttpResponse("Welcome to images page")

环境:

请求方法:POST请求URL:http://localhost:8000/admin/movieaware/cover/add/

Django版本:1.10.3 Python版本:2.7.12已安装的应用程序:

['movieaware.apps.MovieawareConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
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.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

回溯:文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/exception.py”在内部39. response = get_response(request)文件“/ home / kartheek /Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/base.py“在_get_response 187. response = self.process_exception_by_middleware(e,request)File”/ home / kartheek / Applications / anaconda2 / lib / python2.7 / site-packages / django / core / handlers / base.py“在_get_response 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)File”/ home / kartheek / Applications / anaconda2 / lib /包装544中的python2.7 / site-packages / django / contrib / admin / options.py“返回self.admin_site.admin_view(查看)(* args,** kwargs)文件”/ home / kartheek / Applications / anaconda2 / lib / python2.7 / site-packages / django / utils / decorators.py“in _wrapped_view 149. response = view_func(request,* args,** kwargs)File”/ home / kartheek / Applications / anaconda2 / lib / python2 . 7 / site-packages / django / views / decorators / cache.py“我n _wrapped_view_func 57. response = view_func(request,* args,** kwargs)内部文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/sites.py” 211.返回视图(request,* args,** kwargs)add_view 1509中的文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py” self.changeform_view(request,None,form_url,extra_context)_wrapper 67中的文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py” . 返回bound_func(* args) ,** kwargs)文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py”在_wrapped_view 149. response = view_func(request,* args,** kwargs )在bound_func 63中输入文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py” . 返回func.get(self,type(self))(* args2, ** kwargs2)文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decora tors.py“在内部185. return func(* args,** kwargs)文件”/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py“in changeform_view 1453. self_log_addition(request,new_object,change_message)log_addition 719中的文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py” . object_repr = force_text(object),文件“/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/encoding.py”在force_text 78中.s = six.text_type(s)异常类型:TypeError at / admin / movieaware / cover / add / Exception值:强制转换为Unicode:需要很长的字符串或缓冲区

我已经尝试了大多数建议的解决方案,但没有任何工作 . 我无法弄清楚问题所在

2 回答

  • 0
    def __str__(self):
        return self.cover_id
    

    在这里,我返回类型为Long的字符串函数 . 它应该返回一个字符串,这就是它抛出错误的原因 .

  • 0

    可能是空的吗?

    试试这个:

    return unicode(self.cover_path) or u''
    

    如果您尝试通过“unicode”传递对象而不是字符串,也可能发生错误 .

    我们无法检查您的代码,但测试类型(self.cover_path)并确保它是一个字符串,否则手动转换它以确保 .

相关问题