如何使用Django将表单/字段内容上载到IPFS节点?

2024-05-19 07:07:11 发布

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

我想重写django ImageField的上载行为,以便在上传到某个url之后,该文件将被添加到ipfs节点。在

例如,我的模型类似于:

class Profile(models.Model):
    picture = models.ImageField(upload_to=upload_location, blank=True)

我首先尝试像保存其他图像一样保存它,但是我给它一个IPFS散列,这将允许用户加载数据客户端。在

在我看来,我有以下代码来运行ipfs守护进程的实例。在

^{pr2}$

但是,当我尝试运行python manage.py makemigrationsrunserver时,守护进程会运行,但命令的其余部分不会运行

Initializing daemon...
Swarm listening on /ip4/127.0.0.1/tcp/4001
Swarm listening on /ip4/174.56.29.92/tcp/4001
Swarm listening on /ip4/192.168.1.109/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
API server listening on /ip4/127.0.0.1/tcp/5001
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

如何启动ipfs守护进程和django服务器?看起来他们在监听同一个端口(django8000,ipfs8080),那么为什么我会遇到这个问题?在


Tags: 文件djangourl节点server进程onmodels
1条回答
网友
1楼 · 发布于 2024-05-19 07:07:11

https://pydigger.com/pypi/django-ipfs-storage

安装

。。代码::bash

pip install django-ipfs-storage

配置

默认情况下,ipfs_storage向IPFS守护进程添加和固定内容 在本地主机上运行并返回指向公共 https://ipfs.io/ipfs/HTTP网关

要自定义此项,请在settings.py中设置以下变量:

  • IPFS_STORAGE_API_URL:默认为 'http://localhost:5001/api/v0/'。在
  • IPFS_GATEWAY_API_URL:默认为'https://ipfs.io/ipfs/'。在

IPFS_GATEWAY_API_URL设置为'http://localhost:8080/ipfs/'为 通过本地守护进程的HTTP网关提供内容。在

使用

有两种方法可以使用Django存储后端。在

作为默认后端 ~~~~~~~~~~~~~~~~~~~

将IPFS用作Django’s default file storage backend <https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-DEFAULT_FILE_STORAGE>\uuU:

。。代码::python

^{pr2}$

对于特定的FileField ~~~~~~~~~~~~~~~~~~~~~~~~~~

或者,您可能只想将IPFS存储后端用于 单个字段:

。。代码::python

from django.db import models

from ipfs_storage import InterPlanetaryFileSystemStorage 


class MyModel(models.Model):
    # …
    file_stored_on_ipfs = models.FileField(storage=InterPlanetaryFileSystemStorage()) 
    other_file = models.FileField()  # will still use DEFAULT_FILE_STORAGE

相关问题 更多 >

    热门问题