TypeError:强制为Unicode:需要字符串或缓冲区,已找到long

2024-04-26 17:36:44 发布

您现在位置:Python中文网/ 问答频道 /正文

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 已安装的应用程序:

^{pr2}$

Traceback:

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/exception.py" in inner 39. response = get_response(request)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/core/handlers/base.py" in _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" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py" in wrapper 544. return self.admin_site.admin_view(view)(*args, **kwargs)

File "/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" in _wrapped_view_func 57. response = view_func(request, *args, **kwargs)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/sites.py" in inner 211. return view(request, *args, **kwargs)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py" in add_view 1509. return self.changeform_view(request, None, form_url, extra_context)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py" in _wrapper 67. return bound_func(*args, **kwargs)

File "/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/utils/decorators.py" in bound_func 63. return func.get(self, type(self))(*args2, **kwargs2)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/decorators.py" in inner 185. return func(*args, **kwargs)

File "/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)

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/contrib/admin/options.py" in log_addition 719. object_repr=force_text(object),

File "/home/kartheek/Applications/anaconda2/lib/python2.7/site-packages/django/utils/encoding.py" in force_text 78. s = six.text_type(s)

Exception Type: TypeError at /admin/movieaware/cover/add/ Exception Value: coercing to Unicode: need string or buffer, long found

我已经尝试了大多数建议的解决办法,但没有任何效果。 我没能解决这个问题


Tags: djangoinpyhomemodelslibpackagessite
2条回答

可能是空的吗?在

试试这个:

        return unicode(self.cover_path) or u''

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

我们无法检查您的代码,但要测试类型(self.cover_路径)并确保它是字符串,否则手动转换以确保。在

def __str__(self):
    return self.cover_id

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

相关问题 更多 >