我尝试实现用于编辑 Django 管理面板的代码。它运行没有任何错误,但它没有更新数据库中的相关数据字段。这是我更新数据库表的代码。
视图.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 模板
{% 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 %}
请帮我解决这个问题。
`