django - data's are not saving in database -
i using following code in views.py
def when(request): log.debug("test....") reportform = reportform() if request.method == 'post': if reportform.is_valid(): log.debug("test:%s",reportform) reportform = reportform.save() return render_to_response('incident/when.html',{'newreport_menu': true, 'form': reportform },context_instance=requestcontext(request))
my template is
{{ form.manual_date_time }} <p id=ir-nextbutn><a href="{% url incident.views.media %}"> <form action="/member/media/" method="post"> {% csrf_token %} {% include "buttons/next.html" %} </form>
models.py
class report(models.model): user = models.foreignkey(user, null=false) incident_number = models.charfield('incident number', max_length=100) device_id = models.charfield('device id', max_length=100) app_uuid = models.charfield('unique app id', max_length=100) created_date_time = models.datetimefield('created') manual_date_time = models.datetimefield('another time', null=true, blank=true) sent_date_time = models.datetimefield('sent') incident_description = models.textfield('incident description', null=true, blank=true)
here saving date time field in database.after entering time , date in field if user clicks next button save , goes next page.but not storing data.i using mysql db.
you not applying data form cannot vaild.
def when(request): if request.method == 'post': # form post'ed data reportform = reportform( data=request.post, ) if reportform.is_valid(): log.debug("test:%s",reportform) reportform = reportform.save() # should redirect different page @ point # avoid double post'ing , saving redirect_to = request.post.get( 'next', reverse('name-of-url-defined-in-your-urls.py') ) return httpresponseredirect(redirect_to) else: # check errors, *should* appear when form rendered pass else: # blank form, no data reportform = reportform() return render_to_response( 'incident/when.html', { 'newreport_menu': true, 'form': reportform, }, context_instance=requestcontext(request) )
update:
additionally form not encompassed within form
tags. , "next" button needs input
or button
type="submit"
<form action="/member/media/" method="post"> {% csrf_token %} <input name="next" type="hidden" value="{{ url_to_redirect_to }}"/> {{ form }} <input type="submit" name="save" value="save"/> or <button type="submit">save</button> </form>
side note:
you add auto_now_add=true
created_date_time
avoid having set , stop being edited , appearing in modelform
s
created_date_time = models.datetimefield('created', auto_now_add=true,)
Comments
Post a Comment