如何编辑用户配置文件

时间:2012-09-04 07:20:27

标签: django

大家好,我是Django的新手。 我希望当我登录我的帐户时,会有一个编辑按钮,向我显示一些我可以编辑的字段。

我很困惑如何将数据保存到同一个用户配置文件中。

所以有人可以告诉我这是怎么可能的。可以用一个例子给我看看

使用我的profile.html,我可以看到我的个人资料,点击编辑按钮,我可以编辑我的个人资料

{% extends 'base.html' %}
{% block content %}
    <p>User_id: {{ drinker.user_id }}
    <p>Name: {{ drinker.name }}</p>
        <p>Birthday: {{ drinker.birthday }}</p>
        <p>first_name: {{ user.first_name }}</p>     
        <p>Users: {{ user.username }}</p>
    <p><a href="/edit/">Edit Profile</a>
{% endblock %}

修改功能

 def Edit(request):
        if not request.user.is_authenticated():
             return HttpResponseRedirect('/login/') 
        drinker = request.user.get_profile()
        context = {'drinker':drinker}
        return render_to_response('edit.html', context, context_instance=RequestContext(request))

**Edit.html**

{% extends "base.html" %}
{% block extrahead %}
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js" type="text/javascript"></script>
    <script>
    $(function() {
        $( "#birth" ).datepicker();
    });
    </script>
{% endblock %}
{% block content %}
<form action="/edit1/" method="post">
{% csrf_token %}

<div class="register_div">
    <p><label for="name">Name:</label></p>
    <p><input type="text" value="{{ drinker.name }}"></p>
</div>
<div class="register_div">

    <p><label for="birthday">Birthday: {{ drinker.birthday }} </label></p>
        <p>Choose new date of birth</p>
    <p><input type="text" value="" id="birth"></p>
</div>
<p><input type="submit" value="submit" /></p>
</form>
{% endblock %}

在edit1编辑请求功能上工作

def EditRequest(request):
    #if request.method == 'POST':
       #form = UserProfileForm(request.POST, instance=user)

  #if request.user.is_authenticated():
    #return render_to_response('hgdhg')
    if request.method == 'POST':
         form = EditForm(request.POST)
         if form.is_valid():
            user=User.objects.create_user(usere_id=form.cleaned_data['user_id'])
         #user.save()
            drinker=user.get_profile()
            drinker.name=form.cleaned_data['name']
            drinker.birthday=form.cleaned_data['birthday']
            drinker.save()
            return HttpResponseRedirect('/profile/')
         else:
             return HttpResponseRedirect('/f/') 
    else:
            return render_to_response('f')#,{'form':form} , context_instance=RequestContext(request)) 

此editrequest不起作用?

3 个答案:

答案 0 :(得分:1)

以下是编辑用户个人资料时需要执行的步骤:

  1. 找出登录的用户(阅读user authentication
  2. 检查用户是否有个人资料;使用普通的django查询机制。
  3. 如果用户有个人资料;使用配置文件的实例填充ModelForm(请参阅手册中的this page
  4. 将表单显示给最终用户,就像任何其他表单一样。
  5. 当用户提交更改时,请执行常规表单验证并将对象保存到数据库中。
  6. 以下是执行步骤1-4的一些代码:

    from django.shortcuts import render, redirect
    from django.contrib.auth.decorators import login_required
    
    from myapp.models import UserProfile
    from myapp.forms import UserProfileForm 
    
    @login_required
    def edit_profile(request):
        try:
           user_profile = UserProfile.objects.get(user=request.user)
        except UserProfile.DoesNotExist:
           # this user has no profile
           return redirect('/error')
    
        user_profile_form = UserProfileForm(instance=user_profile)
        return render(request,'profile_edit.html',{'form':user_profile_form})
    

    UserProfileForm类:

    from django import forms
    from myapp.models import UserProfile
    
    class UserProfileForm(forms.ModelForm):
        class Meta:
            model = UserProfile
    

答案 1 :(得分:0)

在您的模型表单中传递用户实例

  user = User.objects.get(user_name = username)
  form  = Registrationform(instance=user)

将此表单呈现给您的模板

答案 2 :(得分:0)

我之前做的例子:

@login_required                              
def lexuseditform(request,userpk):
    if Adult.objects.filter(user=request.user).exists():
        adult = Adult.objects.get(user=request.user) # load existing Adult
    else:
        adult = Adult(user=request.user) # create new Adult
    if request.method == 'POST': # If the form has been submitted...
        form = AdultForm(request.POST,instance=adult) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            form.save()
            redirect_url = reverse('lexusedited',kwargs={'userpk': request.user.pk})
            return HttpResponseRedirect(redirect_url) # Redirect after POST
    else:
           form = AdultForm(instance=adult) # An unbound form
    return render(request,'lexus/lexuseditform.html', {'form': form})

@login_required                              
def lexusedited(request,userpk):
    return render(request,'lexus/lexusedited.html')

希望这会有所帮助......