我正處於一個非常奇怪的問題。爲什麼file.url在django中返回與file.path相同的值?
我有聲明如下模型中的默認值的FileField:
class MyModel(models.Model):
name = models.CharField(max_length=32)
audio_file = models.FileField(upload_to='user_menus/', default='%suser_menus/default.mp3' % settings.MEDIA_ROOT, blank=True, null=False)
現在,當我做了以下
>>> a = MyModel(name='Foo')
>>> a.save()
>>> a.audio_file.path
'/full/path/to/file'
>>> a.audio_file.url
'/full/path/to/file' # again
我有我的MEDIA_ROOT和MEDIA_URL配置如下
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
MEDIA_URL = '/media/'
我錯過了什麼嗎? 有什麼建議嗎?
預先感謝您。
您可以在模板中看到差異,它預先顯示url域。這是區別:https://docs.djangoproject.com/en/dev/topics/files/#using-files-in-models – karthikr