Python 从前端插入记录时如何将当前用户自动插入我的数据库

Python 从前端插入记录时如何将当前用户自动插入我的数据库,python,django,django-models,django-views,Python,Django,Django Models,Django Views,我的数据库上有列“月份”和“值”。我正在将月份和值从前端插入数据库。每当我从前端传递月份和值时,我需要将当前用户(用户名)也存储在单独的列中 另一个问题是,当我的月份被输入一次时,我就不能再输入同一个月份了..如何为这个设置验证 我被困在这一层。有人能帮我整理一下吗 Html文件: <html> <head> <title>FRONTEND VALUES BP</title> </head> <body> <p

我的数据库上有列“月份”和“值”。我正在将月份和值从前端插入数据库。每当我从前端传递月份和值时,我需要将当前用户(用户名)也存储在单独的列中

另一个问题是,当我的月份被输入一次时,我就不能再输入同一个月份了..如何为这个设置验证

我被困在这一层。有人能帮我整理一下吗

Html文件:

<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>
views.py

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')

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

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()
在视图中,您需要-:

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')

非常感谢,先生!!另一个问题是,当我的月份被输入一次时,我就不能再输入同一个月份了..如何为这个设置验证??
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')