简体   繁体   中英

Django: how to save image in django?

i am trying to save image in django, but it seems not to be working as expected and i can't really tell what is going with the code. the the way i am submitting the form is not a straight forward way of doing form.save but i am trying to create a topic, if there is no existing topic from the drop down, other informations get saved but the image does not get saved.

views.py


@login_required
def CreateRoom(request):
    topics = Topic.objects.all()
    if request.method == "POST":
        form = ChatRoomForm(request.POST, request.FILES)
        topic_name = request.POST.get("topic")
        topic , created = Topic.objects.get_or_create(name = topic_name )
        
        new_room = Chatroom.objects.create(
            ...
            image = request.POST.get("image" )
            ...
        )

您必须从 request.FILES 中获取图像,将其写入存储后,然后将图像路径设置为模型字段

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM