I tried to implement the code for editing the Django admin panel. It ran without any errors, but it did not update the relevant data fields in the database. Here is my code for updating the database table.
views.py
@login_required
def edit_profile(request):
if request.POST:
if 'logout' in request.POST:
return HttpResponseRedirect('/logout/')
elif 'save' in request.POST:
user = User.objects.get(username= request.user)
user.user=request.POST.get('user')
user.name=request.POST.get('name')
user.address=request.POST.get('address')
user.designation=request.POST.get('designation')
user.email=request.POST.get('email')
user.role=request.POST.get('role')
user.project=request.POST.get('project')
user.task=request.POST.get('task')
user.save()
return HttpResponseRedirect('/view_profile/')
# user.set_password(form.cleaned_data['password'])
#drinker=user.get_profile();
#drinker.name=form.cleaned_data['name']
#drinker.address=form.cleaned_data['address']
#drinker.save()
elif 'cancel' in request.POST:
return HttpResponseRedirect('/view_profile/')
user_profile = request.user.get_profile()
return render_to_response('edit_profile.html',{'profile':user_profile },context_instance=RequestContext(request))
HTML Template
{% block content %}
<form action="." method="post">
{% csrf_token %}
<input type="submit" value="Logout" name="logout" />
<br/>
User:
<input type="text" name="user" value="{{ profile.user}}" /><br />
Name:
<input type="text" name="name" value="{{ profile.name}}" /><br />
Address:
<input type="text" name="address" value="{{ profile.address}}" /><br />
Designation:
<input type="text" name="designation" value="{{ profile.designation}}" /><br />
Email:
<input type="text" name="email" value="{{ profile.email}}" /><br />
Role:
<input type="text" name="role" value="{{ profile.role}}" /><br />
Project:
<input type="text" name="project" value="{{ profile.project}}" /><br />
Task:
<input type="text" name="task" value="{{ profile.task}}" /><br />
<input type="submit" value="Save Changes" name="save" />
<input type="submit" value="Cancel" name="cancel" />
<br/>
</form>
{% endblock %}
Please help me to resolve this problem.
`
Your code
user = User.objects.get(username=request.user)
//equivalent to ex. (jay=1)
Maybe you should change this line to
user = User.objects.get(username=request.user.username)
or
user = User.objects.get(pk=request.user.id)
Based in your code you define fields that are not in the User model
user = User.objects.get(username= request.user)
user.name=request.POST.get('name')
user.address=request.POST.get('address') <-----
user.designation=request.POST.get('designation') <----
user.email=request.POST.get('email')
user.role=request.POST.get('role') <-----
user.project=request.POST.get('project') <----
user.task=request.POST.get('task') <-----
user.save()
I don't know where these fields came from. If you wish to save those information, you must define a separate model for that fields.
UPDATE:
@login_required
def edit_profile(request):
if request.POST:
user = User.objects.get(pk=request.user.id)
user.username=request.POST.get('user')
user.email=request.POST.get('email')
user.save()
employee = Employee.objects.get(user=request.user)
employee.name=request.POST.get('name')
employee.address=request.POST.get('address')
employee.designation=request.POST.get('designation')
employee.role=request.POST.get('role')
employee.project=request.POST.get('project')
employee.task=request.POST.get('task')
employee.save()
return HttpResponseRedirect('/view_profile/')
user_profile = request.user.get_profile()
return render_to_response('edit_profile.html',{
'profile':user_profile
},context_instance=RequestContext(request))
<form action="." method="post">
{% csrf_token %}
User:
<input type="text" name="user" value="{{ profile.user.username}}" /><br />
Name:
<input type="text" name="name" value="{{ profile.name}}" /><br />
Address:
<input type="text" name="address" value="{{ profile.address}}" /><br />
Designation:
<input type="text" name="designation" value="{{ profile.designation}}" /><br />
Email:
<input type="text" name="email" value="{{ profile.email}}" /><br />
Role:
<input type="text" name="role" value="{{ profile.role}}" /><br />
Project:
<input type="text" name="project" value="{{ profile.project}}" /><br />
Task:
<input type="text" name="task" value="{{ profile.task}}" /><br />
<input type="submit" value="Save Changes" name="save" />
<input type="reset" value="Cancel" name="cancel" />
<br/>
</form>
That's the way to save it but sorry I don't know your Employee model fields exactly so I just guess on it based on the variables you use.