python - Model with instance won't save to DB but no error message in Django -
i'm having issue saving form instance. have model foreign key want save - nothing appearing in table , there doesn't appear error message.
the model is:
class aboutme(models.model): mygender = models.charfield(max_length=50, choices = gender_choices) class message(models.model): mysubject = models.charfield(max_length=100) mymessage = models.charfield(max_length=100) myfromid = models.foreignkey(aboutme) class contactform(modelform): class meta: model = message exclude = ('myread', 'mydeleted', 'myspam', 'mydate', 'mytime', 'myfromid', 'mytoid') mymessage = forms.charfield(widget=forms.textarea) def contact(request): myid = request.session["aboutme_id"] sender = aboutme.objects.get(pk=myid) if request.method == "post": message = contactform(request.post, instance=sender) if message.is_valid(): message.save(commit=false) #return httpresponse(myid) message.myread = false message.mydeleted = false message.myspam = false message.mydate = datetime.date.today() message.mytime = timezone.now() message.mytoid = int(1) message.save() message.save_m2m() return redirect('/') else: message = contactform(request.post, instance=sender) return render(request, "contact.html", {'form': message,})
i've tried too:
def contact(request): myid = request.session["aboutme_id"] sender = aboutme.objects.get(pk=myid) if request.method == "post": messageform = contactform(request.post, instance=sender) if messageform.is_valid(): message = messageform.save(commit=false) #message.save(commit=false) #return httpresponse(myid) message.myread = false message.mydeleted = false message.myspam = false message.mydate = datetime.date.today() message.mytime = timezone.now() message.mytoid = int(1) message.save() #message.save_m2m() return redirect('/') else: messageform = contactform( instance=sender) return render(request, "contact.html", {'form': messageform,})
you passing wrong instance form, because form message
model, not aboutme
model.
here corrected version, along additional checks:
you should make sure have valid session. if have
request.session["aboutme_id"]
, session has expired, you'llkeyerror
(as key not in session). usingget
method of dictionaries suppress error. instead, if key doesn't exists, method returnnone
. if session has expired, should not move forward - redirect root.even if session valid - may not contain valid value model. in case,
aboutme.objects.get(pk=myid)
raise exception. prevent that, wrap in try/except block. if object doesn't exists, means value session invalid. delete key , redirect user root.finally, should have condition if form invalid; added check well.
the rest of changes formatting. method names should start lowercase letters, should normal names. class names should in initialcaps.
def contact(request): myid = request.session.get("aboutme_id") if not myid: # session has expired return redirect('/') try: sender = aboutme.objects.get(pk=myid) except aboume.doesnotexist: # invalid id in session del request.session['aboutme_id'] return redirect('/') messageform = contactform(request.post or {}) if request.method == "post": if messageform.is_valid(): message = messageform.save(commit=false) message.myread = false message.mydeleted = false message.myspam = false message.mydate = datetime.date.today() message.mytime = timezone.now() message.mytoid = 1 message.myfromid = sender message.save() return redirect('/') else: # form not valid return render(request, "contact.html", {'form': messageform}) else: # return empty form return render(request, 'contact.html', {'form': messageform})
Comments
Post a Comment