【发布时间】:2019-02-09 06:31:42
【问题描述】:
姜戈
代码views.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}
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)
print(post_data) 中的结果是 {'name': ['luca', 'jeams'], 'description': ['all', 'all2']}.
print(key, value) 中的结果提取值是:
name Luca
name jeams
description all
description all2
我想将此数据保存在数据库中,但它不起作用。我该怎么办?
【问题讨论】:
标签: python django database python-3.x django-models