Python 如何在数据库上保存
德扬戈 代码视图.py:Python 如何在数据库上保存,python,django,database,python-3.x,django-models,Python,Django,Database,Python 3.x,Django Models,德扬戈 代码视图.py: from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt from home.models import Post @csrf_exempt def home(request): context = {'request_method': request.method}
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from home.models import Post
@csrf_exempt
def 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()
代码模型.py
from django.db import models
class Post(models.Model):
name = models.CharField(max_length=150)
description = models.CharField(max_length=150)
打印(post_数据)的结果是{'name':['luca','jeams'],'description':['all','all2']}。
打印中的结果提取值(键,值)
为:
name Luca
name jeams
description all
description all2
我想将此数据保存到数据库中,但它不起作用。我该怎么办呢?先看一看。使用getlist
获取同名值的列表。然后遍历它们并将数据保存在表中
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from home.models import Post
@csrf_exempt
def home(request):
context = {'request_method': request.method}
if request.method == 'POST':
context['request_payload'] = request.POST.dict()
names = request.POST.getlist('name')
descriptions = request.POST.getlist('description')
for i in range(len(names)):
post = Post.objects.create(name=names[i], description=descriptions[i])
if request.method == 'GET':
context['request_payload'] = request.GET.dict()
return render(request, 'your_template_name_here', context)
看看第一个。使用getlist
获取同名值的列表。然后遍历它们并将数据保存在表中
from django.shortcuts import render
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from home.models import Post
@csrf_exempt
def home(request):
context = {'request_method': request.method}
if request.method == 'POST':
context['request_payload'] = request.POST.dict()
names = request.POST.getlist('name')
descriptions = request.POST.getlist('description')
for i in range(len(names)):
post = Post.objects.create(name=names[i], description=descriptions[i])
if request.method == 'GET':
context['request_payload'] = request.GET.dict()
return render(request, 'your_template_name_here', context)
按以下方式替换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)
它适用于您将您的代码替换为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)
这对你有用