Friday, September 29, 2017

Updating an instance.

Problem 1 :
I have a model with  six fields. One is userName and other five are five favourite cities.....I want to update these five cities....but when I update them using instance argument the userName becomes NONE....I am using the id field to update....the cities are updated for the id but the userName is lost. Any help for that..The code I am using is 
"
def Update(request, city_id):
    if request.method == "POST":
        a= City.objects.get(pk= int(city_id))      
        print "User Updated is : ",a.userName.username                        
        f = CityForm(request.POST, instance=a, initial={'userName.': str(a.userName.username)})
        f.save()
        return HttpResponseRedirect('/login/response/'+str(a.userName.username))
    else:
        all_city = City.objects.all()
        for city in all_city:
            if int(city.pk) == int(city_id):
                a = city
        print "User: ",a.userName.username        
        form_class = CityForm(initial={'userName': a.userName})
        return render(request, 'formsub.html', {'form': form_class,'x':a.userName.username,'id':a.id})     
There is an error in the method also...
Console prints this whenever post request is made.
" [29/Sep/2017 18:43:43] "POST /login/update/18/ HTTP/1.1" 302 0
"

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b4a138b1-871b-4c10-9c92-f863604fb071%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment