Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

260
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda