EADST

Add uploading files feature for Django admin

I was writing an article to introduce my latest research work. However, my blog was not able to upload and share PDF files.

Therefore, I added uploading files feature for Django admin. Here are the steps.

1. Add the class in models.py


class File(models.Model):
    file_title = models.CharField(max_length=50, verbose_name=u'File Title', default='')
    file_detail = models.CharField(max_length=200, verbose_name=u'File Detail', default='')
    file_path = models.FileField(max_length=100, verbose_name=u"File", default='',
                                 upload_to='upload/%Y/%m')

    class Meta:
        verbose_name = u'Site File'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.file_title

2. Add the class in admin.py


from .models import File
class FileAdmin(admin.ModelAdmin):
    list_display = ('file_title', 'file_detail', 'file_url', 'file_data')
    readonly_fields = ('file_data', 'file_url',)
    def file_url(self, obj):
        return mark_safe(u'Copy URL by Clicking right button.' % obj.file_path.url)

    def file_data(self, obj):
        file = mark_safe(u'Filename%s' % obj.file_path.url)
        return file
    file_data.short_description = u'File'
    file_url.short_description = u'File URL'


admin.site.register(File, FileAdmin)

3. Migrate database


python manage.py makemigrations
python manage.py migrate
相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
ONNX API ChatGPT icon transformers scipy 域名 PDB GPT4 uwsgi XML InvalidArgumentError Tiktoken CUDA PDF Interview FP8 版权 Diagram RAR Sklearn CC SQLite Hotel BTC LoRA WAN 第一性原理 Miniforge Heatmap git-lfs GIT 公式 RL Bert HuggingFace 云服务器 Proxy llama.cpp LaTeX torchinfo Windows logger Jev SVR Git API网关 Transformers Conda Numpy TensorFlow Streamlit 递归学习法 Statistics BeautifulSoup CTC Freesound Template Attention Firewall SQL FP32 hf Linux Quantize Llama 报税 SAM 财报 FP64 LLM Docker 强化学习 DeepStream Pytorch Land NLTK Translation 多线程 Plate Pickle Rebuttal TTS Jetson Breakpoint Python Mixtral 图形思考法 Paper HaggingFace Animate 音频 搞笑 顶会 Nginx Qwen2 OpenCV 继承 NLP Random Anaconda Card Logo Bipartite 论文 Math ms-swift Tensor Plotly C++ PyCharm FastAPI Hungarian 净利润 Clash ModelScope uWSGI NameSilo Qwen Vim mmap Color Markdown v0.dev Baidu YOLO CEIR FlashAttention PIP Github GoogLeNet Video diffusers CAM 腾讯云 Disk VSCode Domain Quantization Qwen2.5 Dataset UI News Google Pandas Image2Text OCR QWEN MD5 Datetime UNIX Ubuntu WebCrawler 签证 Food Password TSV Distillation Web Crawler Shortcut Harness Base64 多进程 论文速读 Knowledge OpenAI Review Tracking IndexTTS2 FP16 TensorRT Zip VGG-16 Safetensors Claude Cloudreve ResNet-50 Input 关于博主 CSV tqdm COCO PyTorch Search DeepSeek BF16 Agent Michelin Algorithm Pillow Bin v2ray CV printf Hilton LLAMA XGBoost GPTQ Data LeetCode Ptyhon 阿里云 RGB Website Excel Permission Augmentation 算法题 Django Paddle 证件照 CLAP JSON Jupyter 飞书 AI tar Vmess Magnet git SPIE GGML Bitcoin VPN EXCEL Gemma Use 图标
站点统计

本站现有博文338篇,共被浏览972161次

本站已经建立2684天!

热门文章
文章归档
回到顶部