Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在django中,当我提交图像字段时,它会被清除_Python_Django_Social Media - Fatal编程技术网

Python 在django中,当我提交图像字段时,它会被清除

Python 在django中,当我提交图像字段时,它会被清除,python,django,social-media,Python,Django,Social Media,我正在尝试创建一个类似Instagram的社交媒体应用程序,但由于某种原因,当我将图片加载到图像字段并提交时,图像被清除,并表示该字段是必需的 在Django中,用于 views.py 我暂时将默认用户设置为none,因为在我最初创建模型时,它坚持让我选择一个。这就是这个问题的原因吗?如果是这样的话,我该如何摆脱它 Html代码 <form method="POST" enctype="multipart/form-data">

我正在尝试创建一个类似Instagram的社交媒体应用程序,但由于某种原因,当我将图片加载到图像字段并提交时,图像被清除,并表示该字段是必需的

在Django中,用于 views.py

我暂时将默认用户设置为none,因为在我最初创建模型时,它坚持让我选择一个。这就是这个问题的原因吗?如果是这样的话,我该如何摆脱它

Html代码

    <form method="POST" enctype="multipart/form-data">
        {% csrf_token %}
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">Add a Post!</legend>
            {{ form | crispy }}
        </fieldset>
        <div class="form-group">
            <button class="btn btn-outline-info" type="submit">Post</button>                
        </div>
    </form>
forms.py

class CreateNewPost(forms.ModelForm):
    class Meta:
        model = Post
        fields = ["image","caption"]
为了以防万一,URL.py

urlpatterns = [
    path('', views.home, name='home'),
    path('new_post/', views.new_post, name="new_post"),
]
我还尝试使用基于类的视图,这给了我相同的错误,您需要传递到表单,这是一个类似字典的对象,包含上载文件的
上载文件
对象:

@login_required
def new_post(request):
    if request.method == 'POST':
        form = CreateNewPost(request.POST, request.FILES)
        if form.is_valid():
            form.instance.author = request.user
            form.save()
            messages.success(request, 'Post has been created')
            return redirect('home')
    else:
        form = CreateNewPost()
    return render (request, 'posts/post_form.html', {'form':form})
@需要登录\u
def新职位(请求):
如果request.method==“POST”:
form=CreateNewPost(request.POST,request.FILES)
如果form.is_有效():
form.instance.author=request.user
form.save()
messages.success(请求“已创建帖子”)
返回重定向('home')
其他:
form=CreateNewPost()
返回呈现(请求'posts/post_form.html',{'form':form})

注意:通常最好使用来引用用户模型,而不是直接使用。有关更多信息,请参见


我在django管理界面中尝试了同样的方法,但由于我没有选择作者,所以出现了同样的错误。是否有可能自动获取当前作者作为输入?请阅读-总结是,这不是一个理想的方式来解决志愿者,可能会适得其反获得答案。请不要将此添加到您的问题中。天哪,这非常有效!!!非常感谢你的帮助。意义重大!!
urlpatterns = [
    path('', views.home, name='home'),
    path('new_post/', views.new_post, name="new_post"),
]
@login_required
def new_post(request):
    if request.method == 'POST':
        form = CreateNewPost(request.POST, request.FILES)
        if form.is_valid():
            form.instance.author = request.user
            form.save()
            messages.success(request, 'Post has been created')
            return redirect('home')
    else:
        form = CreateNewPost()
    return render (request, 'posts/post_form.html', {'form':form})