Skip to content
Fix Code Error

How to auto insert the current user into my db when inserting records from Frontend

July 3, 2021 by Code Error
Posted By: Anonymous

I have columns Month and Values on my db.Am inserting month and values from front-end to my db.I need my current user(username) also to be stored in a seperate column whenever I pass my month and values from front end.

Another question is that,When my month is entered once ,I should not be able to enter the same month again..How to set validations for this??

I got stuck at this level.Can anyone help me sorting this?

Html file:

<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

from django.db import models



class graphinput(models.Model):

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

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

Solution

you can add a userfield in your model

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

In views you need to -:

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')
Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • How do I include certain conditions in SQL Count
  • How do I limit the number of digits from 6 to 4 in JS?…
  • Convert array to nested JSON object - Angular Material tree
  • How to properly do JSON API GET requests and assign output…
  • Adding a UserCreationForm to html in Django
  • How would I be able to multiple select and pass data in the…
  • Reference - What does this regex mean?
  • Active tab issue on page load HTML
  • state data can not be set using useState in reactjs

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Key Match using accumulators in XSLT3

Next Post:

Google loadbalancer health checkup fails

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error