Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

267
Views
how can i save ModelForm data to database

I know i should probably not open another thread for this question because it has been asked and answered so many times here.Yes I've worked through the tutorials and browsed the web a lot - what I have is mix of what I'm finding here and at other sites but Im having a hard time saving form inputs in the database.

some one should please come to my rescue i have been trying to get this done for 3 days now, its very frustrating.

here are my codes

models.py class QuestionBank(models.Model):

First_Semester ='First_Semester'
Second_Semester ='Second_Semester'
Semesters = ((First_Semester, 'First_Semester'),(Second_Semester, 'Second_Semester'))

level = models.ForeignKey(ClassLevel)
CourseTitle = models.CharField(max_length=50, null=False)
CourseCode = models.CharField(max_length=10, null=False )
CourseUnit = models.IntegerField()
Semester = models.CharField(max_length=20, choices=Semesters, default="Select_Semester")
Date = models.DateField()
question_papers = models.FileField(upload_to = 'QuestionPapers')

def __str__(self):`enter code here`
    return '%s %s %s %s %s %s %s' %(self.level, self.CourseTitle, self.CourseCode, self.CourseUnit, self.Semester, self.Date, self.question_papers )



  forms.py
class QuestionBankForm(forms.ModelForm):
    class Meta:
        model = QuestionBank
        fields = ('level', 'CourseTitle', 'CourseCode', 'CourseUnit', 'Semester', 'Date', 'question_papers' )

views.py
def uploadQpapers(request):
    context = RequestContext(request)
    if request.method == 'POST':
       Qpapers = QuestionBankForm(data=request.POST)

       if Qpapers.is_valid():
           Qpapers.save()

           return render_to_response("Qbank/uploadQpapers.html", {'Qpapers':Qpapers}, context)

       else:
           return HttpResponse('INVALID')

i want to be able to upload past questions and save it to database but its not working there are no error messages becuase only the else statement is being return any time i try to save, I really dont know what to do i need some one to help me. thanks in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

thanks for all the suggestions guys, I finally Got it to work. here is the working view code which was the major problem

def uploadQpapers(request):
    context = RequestContext(request)
    Qpapers = QuestionBankForm(request.POST, request.FILES)
    if request.method == 'POST':


       if Qpapers.is_valid():

           Upload_Qpapers = Qpapers.save(commit=False)

           if 'question_papers' in request.FILES:
               Upload_Qpapers.question_papers = request.FILES['question_papers']
               Qpapers.save()

           return HttpResponse("ALL GOOD DATA SAVED")
       else:
           return HttpResponse("NOT GOOD")

    else:
        Qpapers = QuestionBankForm()
        return render_to_response("Qbank/uploadQpapers.html", {'Qpapers':Qpapers}, context)

my previous code was missing the quest.FILES, and i wasnt saving the uploaded files seperately i do hope this solution help another beginner like me in the future

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!