Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

javascript - How to insert data in multiple tables from single html form using Django and MySQL?

I have created one HTML form for adding Class_name, Subject_name, Topics and Questions for creating Question Paper Set. All tables are interconnected to each other using Foreign Key in my Models. Now I want to insert these data into different tables named as Classes, Subjects, Topics etc from dropdown in HTML form. These are my code below:

models.py

class Classes(models.Model):

    options = (('JEE', 'JEE'),('IEEE', 'IEEE'))
    className = models.CharField(max_length=200, choices=options, null=True)
    status = models.CharField(max_length=50)

class Subjects(models.Model):

    options = (('Physics', 'Physics'),('Chemistry', 'Chemistry'), ('Maths', 'Maths'))
    classes_id = models.ForeignKey(Classes, max_length=200, null=True, on_delete=models.CASCADE)
    subjectName = models.CharField(max_length=500, choices=options, null=True)
    status = models.CharField(max_length=50)

class Section(models.Model):

    options = (('Sec1', 'Sec1'),('Sec2', 'Sec2'), ('Sec3', 'Sec3'))
    classes_id = models.ForeignKey(Classes, max_length=200, null=True, on_delete=models.CASCADE)
    subject_id = models.ForeignKey(Subjects, max_length=200, null=True, on_delete=models.CASCADE)
    sectionName = models.CharField(max_length=500, choices=options, null=True)
    status = models.CharField(max_length=50)

views.py

def createQuestionPaper(request):

    if request.method == 'POST':

        Class = request.POST['Class']
        Subjects = request.POST['Subjects']
        Sections = request.POST['Sections']
        Topics = request.POST['Topics']

        createClass = Classes.objects.create(className=Class, status=1)
        classId = Classes.objects.latest('id')

        createSubjects = Subjects.objects.create(subjectName=Subjects, classes_id_id=classId, status=1)
        subjectId = Subjects.objects.latest('id')

        createSections = Section.objects.create(sectionName=Sections, classes_id_id=classId, 
        subject_id_id=subjectId, status=1)
        sectionsId = Section.objects.latest('id')
       
        return redirect('/showPaper')
    else:
        return render(request, 'exams/createQuestionPaper.html')

createQuestionPaper.html

      <form action="" name="paper" method="POST">
      {% csrf_token %}

      <select name="Class" class="form-control">
        <option selected="" name="Class">Class</option>
        <option value="JEE"> JEE </option>
        <option value="IEEE"> IEEE </option>
        <option value="PET"> PET </option>
        <option value="AIPMT"> AIPMT </option>
      </select><br>

      <select name="Subjects" class="form-control">
        <option selected="" name="Subjects">Subjects</option>
        <option value="Physics"> Physics </option>
        <option value="Chemistry"> Chemistry </option>
        <option value="Mathematics"> Mathematics </option>
      </select><br>

      <select name="Sections" class="form-control">
        <option selected="" name="Sections">Sections</option>
        <option value="Section 1"> Section 1 </option>
        <option value="Section 2"> Section 2 </option>
        <option value="Section 3"> Section 3 </option>
      </select><br>
      </form>

Please help me out that how I can achieve this.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...