我在django上传图片时遇到问题

时间:2014-07-15 16:33:26

标签: python django

这是models.py文件。虽然你可以看到我已经使用MEDIA_URL作为要保存的图像的目录,但我也搞乱了所有其他替代方法获得类似的错误消息。这可能与权限有关吗?我不确定。

from django.db import models
from taggit.managers import TaggableManager

class  Post(models.Model):
title = models.CharField(max_length = 100)
body = models.TextField()
created = models.DateTimeField()
image1 = models.ImageField(upload_to = MEDIA_URL)



def __unicode__(self):
    return self.title

这是我添加的设置部分。

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

MEDIA_URL = '/media/'

这是我在cmd中使用的错误消息(使用Windows)

Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 284, in execute
self.validate()
File "C:\Python34\lib\site-packages\django\core\management\base.py", line 310, in validate
num_errors = get_validation_errors(s, app)
File "C:\Python34\lib\site-packages\django\core\management\validation.py", line 34, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Python34\lib\site-packages\django\db\models\loading.py", line 196, in get_app_errors
self._populate()
File "C:\Python34\lib\site-packages\django\db\models\loading.py", line 75, in _populate
self.load_app(app_name, True)
File "C:\Python34\lib\site-packages\django\db\models\loading.py", line 99, in load_app
models = import_module('%s.models' % app_name)
File "C:\Python34\lib\importlib\__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "C:\Users\Neema\sonder\blog\models.py", line 4, in <module>
class  Post(models.Model):
File "C:\Users\Neema\sonder\blog\models.py", line 8, in Post
image1 = models.ImageField(upload_to = MEDIA_URL)
NameError: name 'MEDIA_URL' is not defined

2 个答案:

答案 0 :(得分:0)

我认为您需要为models.py添加导入内容:

from django.conf import settings

然后你可以使用它:

settings.MEDIA_URL

无论如何,我不认为将MEDIA_URL添加到您的字段是一个好习惯。如果您将来决定更改它,则需要更新所有数据库条目。

答案 1 :(得分:0)

不要在models.py中提及MEDIA_URL。只需提及一个子目录名称。例如,

image1 = models.ImageField(upload_to="blogimages")

确保媒体文件夹具有服务器(Apache或IIS)的写访问权限。这是Windows中的常见问题。右键单击文件夹并更改权限即可轻松修复。

如果您仍然收到错误,请同时发布。