1

我尝试实现用于编辑 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 %}

请帮我解决这个问题。

`

4

4 回答 4

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 = 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 模型字段,所以我只是根据您使用的变量猜测它。

于 2013-04-12T10:20:14.623 回答
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)

现在您可以通过填写完整的表单来传递此表单

于 2014-12-15T19:05:21.583 回答
0

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

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

......可能只是......

user = request.user

...但我可能是错的。

更新

我突然想到,如果包含该行的代码的原始版本...

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

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

于 2013-04-12T10:23:52.460 回答
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(r'profileedit/(?P<username>[a-z-\d-]+)/$', ProfileView.as_view(), name='profile_view')

模板很明显,我猜

于 2013-04-12T12:45:32.577 回答