ImageField 未上传到指定目录

ImageField is not uploading to specified directory

我一直在用谷歌搜索这个问题,但找不到解决方案。 当我使用管理面板时,图像正在加载到所需的目录中。但是当我从前端创建 post 时,图像只是加载到主目录,即媒体。我现在知道如果我使用 django 表单和 createview 它会工作,但我需要像我一样添加一个 post 和一个函数。我认为这个问题可能会发生,因为 ImageField 需要验证。但我不知道如何在 django 表单上下文之外验证它。

我有带 ImageField 图像和 upload_to 选项的模型帖子:

class Posts(models.Model):
    image = models.ImageField(upload_to="posts/%Y/%m/%d", null=True,blank=True)

然后在 views.py 函数中添加新的 post:

@login_required
def add_post(request):
        
    if request.method == 'POST':

        title = request.POST.get('title')
        content = request.POST.get('content')
        
        upload_file = request.FILES['image']
        fs = FileSystemStorage()

        image = fs.save(upload_file.name, upload_file)
        
        post = Posts(title=title, content=content, image= image)
        post.author = request.user
                
        post.save()

        from django.http import HttpResponseRedirect

        #return redirect(post.get_absolute_url())
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))

    return render(request ,'users/user-detail2.html')

并显示表格:

<form action="{% url 'add_post' %}" method="post" enctype="multipart/form-data">
    {% csrf_token %}
    <div class="modal-header">
        <h5 class="modal-title">Share Your Mood</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>

    <div class="modal-body custom-scroll">
        <input type="text" name="title" placeholder="Post title" class="form-control">
    </div>

    <div class="modal-body custom-scroll">
        <input type="file" id="image" name="image" rows="5" class="form-control">
    </div>

    <div class="modal-body custom-scroll">
        <textarea name="content" class="share-field-big custom-scroll" placeholder="Say Something"></textarea>
    </div>
    <div class="modal-footer">
        <button type="button" class="post-share-btn" data-dismiss="modal">cancel</button>
        
        <input type="submit" value="Post" class="post-share-btn">
    </div>    
</form>

settings.py

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

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

主要urls.py

if settings.DEBUG:

    urlpatterns += static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

这里不需要使用FileSystemStorageclass。只需将文件保存如下

from django.http import HttpResponseRedirect


@login_required
def add_post(request):
    if request.method == 'POST':
        title = request.POST.get('title')
        content = request.POST.get('content')
        <b>upload_file = request.FILES['image']
        post = Posts(title=title, content=content, image=upload_file, author=request.user)
        post.save()</b>
        return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
    return render(request, 'users/user-detail2.html')

我不确定是什么原因造成的

尝试将其添加到您的模型中

from django.core.files.storage import FileSystemStorage

media = FileSystemStorage(location='/media/posts/%Y/%m/%d')