首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在前台插入记录时自动将当前用户插入我的数据库

如何在前台插入记录时自动将当前用户插入我的数据库
EN

Stack Overflow用户
提问于 2021-05-30 23:42:42
回答 1查看 30关注 0票数 1

我在db.Am上有Month和Values列,将month和values从前端插入到数据库中。每当我从前端传递month和values时,我需要将当前用户(用户名)存储在单独的列中。

另一个问题是,当我的月份输入一次时,我应该不能输入同月again..How来设置此验证??

我被这个level.Can卡住了,有人能帮我整理一下吗?

Html文件:

代码语言:javascript
复制
<html>
<head>
    <title>FRONTEND VALUES BP</title>
</head>
<body>
<p>BLOOD PRESSURE</p>
<form action="index" method="post">
    {% csrf_token %}
    <label>Month1:</label>
    <input type="text" name="JanMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="JanValue" placeholder="enter your value"></br></br>

    <label>Month2:</label>
    <input type="text" name="FebMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="FebValue" placeholder="enter your value"></br></br>

    <label>Month3:</label>
    <input type="text" name="MarMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="MarValue" placeholder="enter your value"></br></br>

    <label>Month4:</label>
    <input type="text" name="AprMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="AprValue" placeholder="enter your value"></br></br>

    <label>Month5:</label>
    <input type="text" name="MayMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="MayValue" placeholder="enter your value"></br></br>

    <label>Month6:</label>
    <input type="text" name="JunMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="JunValue" placeholder="enter your value"></br></br>

    <label>Month7:</label>
    <input type="text" name="JulMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="JulValue" placeholder="enter your value"></br></br>

    <label>Month8:</label>
    <input type="text" name="AugMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="AugValue" placeholder="enter your value"></br></br>

    <label>Month9:</label>
    <input type="text" name="SepMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="SepValue" placeholder="enter your value"></br></br>

    <label>Month:10</label>
    <input type="text" name="OctMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="OctValue" placeholder="enter your value"></br></br>

    <label>Month11:</label>
    <input type="text" name="NovMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="NovValue" placeholder="enter your value"></br></br>

    <label>Month12:</label>
    <input type="text" name="DecMonth" placeholder="enter your month"></br></br>
    <label>Value:</label>
    <input type="text" name="DecValue" placeholder="enter your value"></br></br>
    <input type="submit" name="sign-in" value="Sign In">
    <a href="input/graphvalue">
        <button type="button"><span>GRAPH</span></button>
    </a>
</form>

models.py

代码语言:javascript
复制
from django.db import models



class graphinput(models.Model):

  Month = models.CharField(max_length=30)
  Values = models.IntegerField()

views.py

代码语言:javascript
复制
from django.shortcuts import render,redirect
from input.models import graphinput
from.utils import get_plot



def index(request):

 if request.method == 'POST':
     JanMonth = request.POST['JanMonth']
     JanValue = request.POST['JanValue']

     FebMonth = request.POST['FebMonth']
     FebValue = request.POST['FebValue']

     MarMonth = request.POST['MarMonth']
     MarValue = request.POST['MarValue']

     AprMonth = request.POST['AprMonth']
     AprValue = request.POST['AprValue']

     MayMonth = request.POST['MayMonth']
     MayValue = request.POST['MayValue']

     JunMonth = request.POST['JunMonth']
     JunValue = request.POST['JunValue']

     JulMonth = request.POST['JulMonth']
     JulValue = request.POST['JulValue']

     AugMonth = request.POST['AugMonth']
     AugValue = request.POST['AugValue']

     SepMonth = request.POST['SepMonth']
     SepValue = request.POST['SepValue']

     OctMonth = request.POST['OctMonth']
     OctValue = request.POST['OctValue']

     NovMonth = request.POST['NovMonth']
     NovValue = request.POST['NovValue']

     DecMonth = request.POST['DecMonth']
     DecValue = request.POST['DecValue']
     
     graphplot = graphinput.objects.create(Month=JanMonth,Values=JanValue)
     graphplot = graphinput.objects.create(Month=FebMonth,Values=FebValue)
     graphplot = graphinput.objects.create(Month=MarMonth,Values=MarValue)
     graphplot = graphinput.objects.create(Month=AprMonth,Values=AprValue)
     graphplot = graphinput.objects.create(Month=MayMonth,Values=MayValue)
     graphplot = graphinput.objects.create(Month=JunMonth,Values=JunValue)
     graphplot = graphinput.objects.create(Month=JulMonth,Values=JulValue)
     graphplot = graphinput.objects.create(Month=AugMonth,Values=AugValue)
     graphplot = graphinput.objects.create(Month=SepMonth,Values=SepValue)
     graphplot = graphinput.objects.create(Month=OctMonth,Values=OctValue)
     graphplot = graphinput.objects.create(Month=NovMonth,Values=NovValue)
     graphplot = graphinput.objects.create(Month=DecMonth,Values=DecValue)
     graphplot.save();
     print('SUCCESFULLY ADDED')
     return redirect('graphvalue')

 else:
    return render(request, 'bp.html')
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-31 15:02:34

您可以在模型中添加用户字段

代码语言:javascript
复制
from django.db import models
from django.contrib.auth.models import User



class graphinput(models.Model):
  user=models.ForeignKey(User,on_delete=models.CASCADE)
  Month = models.CharField(max_length=30)
  Values = models.IntegerField()

在视图中,您需要-:

代码语言:javascript
复制
from django.shortcuts import render,redirect
from input.models import graphinput
from.utils import get_plot



def index(request):

 if request.method == 'POST':
     JanMonth = request.POST['JanMonth']
     JanValue = request.POST['JanValue']

     FebMonth = request.POST['FebMonth']
     FebValue = request.POST['FebValue']

     MarMonth = request.POST['MarMonth']
     MarValue = request.POST['MarValue']

     AprMonth = request.POST['AprMonth']
     AprValue = request.POST['AprValue']

     MayMonth = request.POST['MayMonth']
     MayValue = request.POST['MayValue']

     JunMonth = request.POST['JunMonth']
     JunValue = request.POST['JunValue']

     JulMonth = request.POST['JulMonth']
     JulValue = request.POST['JulValue']

     AugMonth = request.POST['AugMonth']
     AugValue = request.POST['AugValue']

     SepMonth = request.POST['SepMonth']
     SepValue = request.POST['SepValue']

     OctMonth = request.POST['OctMonth']
     OctValue = request.POST['OctValue']

     NovMonth = request.POST['NovMonth']
     NovValue = request.POST['NovValue']

     DecMonth = request.POST['DecMonth']
     DecValue = request.POST['DecValue']
     
     graphplot = graphinput.objects.create(Month=JanMonth,Values=JanValue,user=request.user)
     graphplot = graphinput.objects.create(Month=FebMonth,Values=FebValue,user=request.user)
     graphplot = graphinput.objects.create(Month=MarMonth,Values=MarValue,user=request.user)
     graphplot = graphinput.objects.create(Month=AprMonth,Values=AprValue,user=request.user)
     graphplot = graphinput.objects.create(Month=MayMonth,Values=MayValue,user=request.user)
     graphplot = graphinput.objects.create(Month=JunMonth,Values=JunValue,user=request.user)
     graphplot = graphinput.objects.create(Month=JulMonth,Values=JulValue,user=request.user)
     graphplot = graphinput.objects.create(Month=AugMonth,Values=AugValue,user=request.user)
     graphplot = graphinput.objects.create(Month=SepMonth,Values=SepValue,user=request.user)
     graphplot = graphinput.objects.create(Month=OctMonth,Values=OctValue,user=request.user)
     graphplot = graphinput.objects.create(Month=NovMonth,Values=NovValue,user=request.user)
     graphplot = graphinput.objects.create(Month=DecMonth,Values=DecValue,user=request.user)
     graphplot.save();
     print('SUCCESFULLY ADDED')
     return redirect('graphvalue')

 else:
    return render(request, 'bp.html')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67762989

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档