0

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

5
  • check by print request.POST whether all fields are present or not Commented Apr 24, 2017 at 17:58
  • 1
    also you can access form.errors in template. Commented Apr 24, 2017 at 17:58
  • thanks for the response but Im still getting errors, when i include print request.POST as suggested by itzmeontv, Im get this error 'The view Qbank.views.uploadQpapers didn't return an HttpResponse object. It returned None instead.' probably because i don know how or where to insert the print statement. Commented Apr 24, 2017 at 18:29
  • i tried verifying if the form was sending any data at all with this def uploadQpapers(request): context = RequestContext(request) if request.method == 'POST': Qpapers = QuestionBankForm(request.POST, request.FILES) if Qpapers.is_valid(): return HttpResponse("ALL GOOD") Im get this error 'The view Qbank.views.uploadQpapers didn't return an HttpResponse object. It returned None instead. Commented Apr 24, 2017 at 18:38
  • from what i have observed so far i think the form is not being validated. i.e not valid. some one should please look at my codes and tell me what is wrong Commented Apr 24, 2017 at 19:09

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.