Django edit and update data with MySql -
in website, each user has own login id , password, if user logged in, can add, edit , update record only.
models.py is
class report(models.model): user = models.foreignkey(user, null=false) name = models.charfield(max_length=20, null=true, blank=true)
views.py
def profile(request): if request.method == 'post': reportform = reportform(request.post) if reportform.is_valid(): report = reportform.save(commit=false) report.user = request.user report.save() return redirect('/index/') else: report = report.objects.get() reportform = reportform(instance=report) return render_to_response('report/index.html', { 'form': reportform, }, context_instance=requestcontext(request))
a user should have 1 age , should in 1 row of data in database.
- if there no data in database, i.e if it's first time user, allowed user insert data database.
- if user reopens page, data inserted should shown in editable mode.
it scenario "if new user creates gmail account, can create account @ first time , again can edit , update details".the same procedure want implement in website.
i tried above code, not able insert data in database. tried direct insertion in mysql db , checked, inserted data can see in editable mode if change , save, creating row of data in db.
then if going insert first time, getting following trace back.
environment: request method: request url: http://192.168.100.10/report/index/ django version: 1.3.7 python version: 2.7.0 installed applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.admindocs', 'django.contrib.humanize', 'django.contrib.staticfiles', 'south', 'collect', 'incident', 'report_settings'] installed middleware: ('django.middleware.common.commonmiddleware', 'django.contrib.sessions.middleware.sessionmiddleware', 'django.middleware.csrf.csrfviewmiddleware', 'django.contrib.auth.middleware.authenticationmiddleware', 'django.contrib.messages.middleware.messagemiddleware', 'django.middleware.transaction.transactionmiddleware', 'django.middleware.cache.fetchfromcachemiddleware') traceback: file "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) file "/root/projects/ir/incident/views.py" in when 559. report = report.objects.get() file "/usr/lib/python2.7/site-packages/django/db/models/manager.py" in 132. return self.get_query_set().get(*args, **kwargs) file "/usr/lib/python2.7/site-packages/django/db/models/query.py" in 349. % self.model._meta.object_name) exception type: doesnotexist @ report/index/ exception value: report matching query not exist.
based on code you've shown, issue report
did not saved database. haven't said reportform
using, or data used that, following part never executed:
if reportform.is_valid(): report = reportform.save(commit=false) (...)
hence, no report ever saved. because of this, when reach line:
report = report.objects.get()
... traceback mention. i'd recommend replacing .get()
either .all()
or .filter()
, won't throw exception when there no matching results.
for updating data, should first report instance database, , pass form's save
function:
report = report.objects.filter(name=some_value) report.user = request.user reportform.save(instance=report)
Comments
Post a Comment