Django Updating Existing Model field -


i have model in django foreign key django user model. trying update model form, database isn't updating. can't figure out problem.

model.py

from django.conf import settings      class userinfo(models.model):         username = models.charfield(max_length = 30)         owner =  models.foreignkey(settings.auth_user_model,on_delete=models.cascade,) 

form.py

from django import forms society.models import userinfo      class editform(forms.modelform):         username=forms.charfield(widget=forms.textinput(attrs={'onchange': 'this.form.submit();', 'class': 'editinput'}))         class meta:             model = userinfo             fields ='__all__' 

views.py

 django.shortcuts import render     society.models import userinfo     django.contrib.auth.models import user     society.forms import editform          def profileview(request):             user = request.user             username = userinfo.objects.get(owner=user)             if request.method == 'post':                 form = editform(request.post, instance=username)                 if form.is_valid():                     form.save()             else:                 form = editform(instance=username)               return render (request, 'profile_view.html', {'user':username, 'form':form}) 

url.py

from django.conf.urls import url django.contrib import admin import society.views      urlpatterns = [         url(r'^$', 'society.views.home'),         url(r'^admin/', admin.site.urls),         url(r'^login/', 'django.contrib.auth.views.login'),         url(r'^logout/', 'django.contrib.auth.views.logout'),         url(r'^userreg/', 'society.views.userreg'),         url(r'^profile/', 'society.views.profileview'),     ] 

profile_view.html

    <div>         <form method="post">         {% csrf_token %}             {{form.username}}             {{user.username}}         </form>     </div> 

when ever change form.username, user.username change instantly database not updating. tried adding submit button, still no luck.

you should pass record id updating existing userinfo record.

from django.shortcuts import render society.models import userinfo django.contrib.auth.models import user society.forms import editform  def profileview(request):     user_id = request.post.get('user_id')     user = userinfo.objects.get(pk=user_id)     if request.method == 'post':         form = editform(request.post, instance=user)         if form.is_valid():             form.save()     else:         form = editform(instance=user)      return render (request, 'profile_view.html', {'user':username, 'form':form}) 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -