Django admin:如何允许用户编辑个人资料?

时间:2013-04-12 09:46:15

标签: django django-models django-admin django-profiles

我尝试实现编辑Django管理面板的代码。它运行没有任何错误,但它没有更新数据库中的相关数据字段。这是我更新数据库表的代码。

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模板

     {% 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 %}

请帮我解决这个问题。

`

4 个答案:

答案 0 :(得分:1)

您的代码

user = User.objects.get(username=request.user)
//equivalent to ex. (jay=1)

也许您应该将此行更改为

user = User.objects.get(username=request.user.username)

user = User.objects.get(pk=request.user.id)

根据您的代码,您可以定义不在用户模型中的字段

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()

我不知道这些领域来自哪里。如果要保存这些信息,则必须为这些字段定义单独的模型。

<强>更新

@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>

这是保存它的方法,但很遗憾我不完全了解您的Employee模型字段,所以我只是根据您使用的变量猜测它。

答案 1 :(得分:0)

如果我没记错的话,request.user是发出请求的用户的Django模型的实例,所以行......

user = User.objects.get(username= request.user)

......可能就是......

user = request.user

......但我可能错了。

<强>更新

在我看来,如果代码的原始版本,其中包含行...

user = User.objects.get(username= request.user)

...没有引发异常,那么它甚至可能没有被执行。如果您从Django内置runserver命令运行,请尝试在函数顶部打印出request变量的内容。

答案 2 :(得分:0)

class ProfileView(UpdateView):
    model = User
    template_name = 'profile.html'
    slug_url_kwarg = 'username'
    slug_field = 'username'

    def get_object(self, queryset=None):
        obj = super(ProfileView, self).get_object(queryset)
        if not self.request.user or self.request.user.pk != obj.pk:
            raise Http404('nope')
        return obj

url route:

url(r'profileedit/(?P<username>[a-z-\d-]+)/$', ProfileView.as_view(), name='profile_view')

模板很明显,我想

答案 3 :(得分:0)

您可以像这样创建一个新表单(myExample):

class userForm(ModelForm):

    class Meta:
        model = Usuario
        fields = ['userName','name', 'lastName', 'email', 'description', 'image']}

然后在您的视图文件和您的方法中添加具有对象实例的表单,如:

myUser = User.objects.get(id=request.session['userId'])
form = userForm(request.POST, instance=myUser)

现在您可以传递此表单并填写

字段