django编辑页面并更新

时间:2013-04-13 08:53:48

标签: python django

我有一个HTML页面,显示从我的数据库中提取的已存储的详细信息。我必须编辑我的详细信息,细节必须替换我的旧内容id db(即; overwitten到同一个字段)。我正在使用MySQL db。我添加了我的代码。当我提交表单时,它没有更新到db。检查这段代码是否正确?

models.py

class js_details(models.Model):
    user      = models.ForeignKey(User, unique=True)
    #PERSONAL INFO
    fname     = models.CharField(max_length=30)
    contactno = models.IntegerField(max_length=30)
    emailid   = models.EmailField(max_length=30)
    dob       = models.IntegerField(max_length=30)

forms.py

class EditForm(forms.ModelForm):
class Meta:
    model = js_details
    exclude = ('user')

views.py

def editresume(request):
    user = request.user
    profile_info = js_details.objects.filter(user_id=user)
    profile_data = { "profile_detail" : profile_info }
    if request.method == "POST":
        edit = EditForm(request.POST, request.FILES, instance = request.user.get_profile())

        if edit.is_valid():
            edit.save()        
            return HttpResponseRedirect('/accounts/Profile/')
    else:
        edit = EditForm(instance = request.user.get_profile())
    return render_to_response('registration/personal_information.html', profile_data, context_instance=RequestContext(request))

personal_information.html

<h4> Personal Details</h4>
<table width="90%" border="0">
    <tr>
        <td width="30%">
            <p> Name </p>
            <p> Contact Number </p>
            <p> Email ID</p>
            <p>DateOfBirth</p>
        </td>
        <td width="30%">
            <p>{% for detail in profile_detail %}{{ detail.fname}} {{ detail.lastname}}{% endfor %}</p>
            <p>{% for detail in profile_detail %}{{ detail.pri_contact }}{% endfor %}</p>
            <p>{{ user.email }}</p>
            <p>{% for detail in profile_detail %}{{ detail.dob }}{% endfor %}</p>
        </td>
        <td width="30%">
            <p><a href="">Edit</a></p>
        </td>
    </tr>
</table>

2 个答案:

答案 0 :(得分:2)

好的,您希望显示带有表单的html页面,允许用户更改其设置。

所以我们想做的是:

  1. 如果有请求(即用户发送了表单),请进行处理。
  2. 如果没有请求,请显示表单。
  3. Django已经有built-in form library,可以帮助您(1)显示表单和(2)检查数据是否有错误。这是一个简单的tutorial来处理它。

    现在这是您可以使用的伪代码。

    #views.py

    if there is a request (request.method == "POST")
        get your form
        if the data is correct (form.is_valid())
           retreive the data from your form
           save it it your db
        else
           render your html and pass the form you retrieved (that will display errors)
    else
        render and pass a blank form
    

    #forms.py

    class myform
        different inputs
    

    #html文件

    {% if form.errors %}
        <p style="color: red;">{{ form.errors}}</p>
    {% endif %}
    
    <form action="." method="POST">{% csrf_token %}
        {{ form.as_p }}
    <input type="submit" name="formname" value="Edit !">
    </form>
    

    告诉我您是否需要更多详细信息。

答案 1 :(得分:0)

您需要为js_details定义Form o ModelForm。此表单允许用户编辑js_details数据。然后,定义一个处理此表单的新视图,以更新以前表单中的数据库数据。从profile_edit.html所需的表单中的帖子操作引用此新视图。

相关问题