第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何保存數(shù)據(jù)庫

如何保存數(shù)據(jù)庫

一只萌萌小番薯 2021-06-02 21:01:30
代碼views.py:from django.shortcuts import renderfrom django.http import HttpResponsefrom django.views.decorators.csrf import csrf_exemptfrom home.models import Post@csrf_exemptdef home(request):    context = {'request_method': request.method}    if request.method == 'POST':        context['request_payload'] = request.POST.dict()        post_data = dict(request.POST)        print(post_data)         for key, value in post_data.items():            for subvalue in value:                print(key, subvalue)                if key == 'name':                    m = Post.name(subvalue)                    m.save()                if key == 'description':                    print(subvalue)                    p = Post.description(subvalue)                    p.save()    if request.method == 'GET':        context['request_payload'] = request.GET.dict()    return HttpResponse()代碼模型.pyfrom django.db import modelsclass Post(models.Model):    name = models.CharField(max_length=150)    description = models.CharField(max_length=150)結(jié)果中print(post_data)的{'name': ['luca', 'jeams'], 'description': ['all', 'all2']}. 結(jié)果提取值print(key, value)是:name Lucaname jeamsdescription alldescription all2我想將這些數(shù)據(jù)保存在數(shù)據(jù)庫中,但它不起作用。我能怎么做?
查看完整描述

2 回答

?
守著星空守著你

TA貢獻1799條經(jīng)驗 獲得超8個贊

替換您的 post 方法代碼,如下所示:


if request.method == 'POST':

for ind, obj in enumerate(request.POST.getlist('name')):

    desc = request.POST.getlist('description')[ind]

    Post.object.create(name=obj, description=desc)

這對你有用


查看完整回答
反對 回復(fù) 2021-06-09
  • 2 回答
  • 0 關(guān)注
  • 140 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號