-3

大家好,我是 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)) 

这个编辑请求不起作用?

4

3 回答 3

1

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

  1. 找出登录的用户(阅读用户身份验证
  2. 检查用户是否有个人资料;为此使用正常的 django 查询机制。
  3. 如果用户有个人资料;使用配置文件的实例填充 ModelForm(请参阅手册中的此页面
  4. 像任何其他表单一样向最终用户显示表单。
  5. 当用户提交更改时,进行正常的表单验证并将对象保存到数据库中。

这是执行步骤 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
于 2012-09-04T07:36:11.873 回答
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')

希望这可以帮助...

于 2012-09-04T08:02:06.697 回答
0

沿模型表单传递用户实例

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

并将此表单呈现给您的模板

于 2012-09-04T07:39:43.090 回答