Django中Python 2.7的TypeError
嘿,我刚开始学Django,需要一些帮助。当我把我的模型添加到Django的管理界面时,一切看起来都很好,但当我尝试在数据库中添加或删除一条记录时,我遇到了这个问题:
TypeError at /admin/Users/user/add/
coercing to Unicode: need string or buffer, tuple found
我在网上搜索了一下,然后在我的用户模型类的末尾添加了:
def __str__(self):
return ""
但是没有成功。我不确定是否需要在admin.py文件里添加什么?我的用户类里也没有“add”方法,除了上面提到的方法外没有其他返回。
谢谢大家的帮助!
用户类:
class User(models.Model):
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
username = models.CharField(max_length=30)
email = models.EmailField()
password = models.CharField(max_length=30)
birth_date = models.DateField()
description = models.CharField(max_length=200)
gender = models.CharField(max_length = 1, choices = GENDER_CHOICES, default = "M")
image = models.ImageField(upload_to="media/photos/")
signupIP = models.IPAddressField()
privateOrPublic = models.BooleanField(default=1)
def __str__(self):
return ""
还有在/Users/里的简单admin.py:
from Users.models import User
from django.contrib import admin
admin.site.register(User)
错误追踪信息:
Environment:
Request Method: POST
Request URL: http://127.0.0.1/admin/Users/user/add/
Django Version: 1.2.3
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.admin',
'Users']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in wrapper
239. return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
69. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in inner
190. return view(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapper
21. return decorator(bound_func)(*args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in _wrapped_view
76. response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\utils\decorators.py" in bound_func
17. return func(self, *args2, **kwargs2)
File "C:\Python27\lib\site-packages\django\db\transaction.py" in _commit_on_success
299. res = func(*args, **kw)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in add_view
795. self.save_model(request, new_object, form, change=False)
File "C:\Python27\lib\site-packages\django\contrib\admin\options.py" in save_model
597. obj.save()
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save
434. self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "C:\Python27\lib\site-packages\django\db\models\base.py" in save_base
517. for f in meta.local_fields if not isinstance(f, AutoField)]
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in pre_save
255. file.save(file.name, file, save=False)
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in save
91. name = self.field.generate_filename(self.instance, name)
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in generate_filename
282. return os.path.join(self.get_directory_name(), self.get_filename(filename))
File "C:\Python27\lib\site-packages\django\db\models\fields\files.py" in get_filename
279. return os.path.normpath(self.storage.get_valid_name(os.path.basename(filename)))
File "C:\Python27\lib\site-packages\django\utils\functional.py" in __getattr__
276. self._setup()
File "C:\Python27\lib\site-packages\django\core\files\storage.py" in _setup
242. self._wrapped = get_storage_class()()
File "C:\Python27\lib\site-packages\django\core\files\storage.py" in __init__
133. self.location = os.path.abspath(location)
File "C:\Python27\lib\ntpath.py" in abspath
465. path = _getfullpathname(path)
Exception Type: TypeError at /admin/Users/user/add/
Exception Value: coercing to Unicode: need string or buffer, tuple found
1 个回答
1
在你的 MEDIA_ROOT 定义中,把替换的内容改成原始字符串。否则,你会替换掉一个实际的单个反斜杠,而不是你想要的两个反斜杠。
MEDIA_ROOT = os.path.join(os.path.dirname(file), "media").replace(r"\\", "//")