用户应将他的护照数据上传到网站。此数据是私有的,因此如果将其放置在服务器目录之外会更好。
这是一个模型:
passport_storage = FileSystemStorage(setting('ROOT_PATH'))
class Passport(models.Model):
user = models.OneToOneField(User)
scan = models.ImageField(verbose_name=u'Scan or photo', storage=passport_storage, upload_to='passport', blank=True)
def preview_scan(self):
return '<a href="%s"> <img src="%s%s/?thumb=1"></img> </a>' % (self.id, PASSPORT_URL, self.id)
preview_scan.allow_tags = True
我写了获取护照的查看方法,或者是拇指:
@login_required
def get_passport(request, **kwargs):
id = kwargs.pop('id', None)
user = request.user
passport = Passport.objects.filter(pk=id)
if user.username != 'admin': # говнокод
passport = passport.filter(user=user)
passport = passport[0]
scan = passport.scan
path = scan.path
sz = scan.size
if request.GET.get('thumb', None):
path = get_thumb(path)
sz = os.path.getsize(path)
filename = os.path.basename(path)
mt = mimetypes.guess_type(filename)[0]
wrapper = FileWrapper(file(path))
response = HttpResponse(wrapper, mimetype=mt)
response['Content-Disposition'] = 'attachment; filename=%s' % filename
response['Content-Length'] = sz
return response
但是来自 Passport 模型的 ModelForm 显示来自/media/
. 我在哪里可以更改表单的 ImageField 链接?