Py学习  »  问与答

请教一个view问题。

415293799 • 8 年前 • 2335 次点击  

我现在在自学django。现在遇到一个问题。 不能上传图片,我文字描述了。我前端是这样的。 ————— —————— —————— ——————— ————————— ————————— ——— ———— ———— test | test | test | test1 | 1.1.1.1 | 1.1.1.1 |详情| 编辑| 删除 | ————— ————— —————— ——————— ————————— ————————— ——— ———— ————

我现在要点击“编辑” 对这个数据进行更新。 “编辑”这里我是一个get请求。Url如下: http://127.0.0.1:8000/lnasset/hostedit/?id=16

然后就是看到编辑页面了。 这里的url.py: url(r'^lnasset/hostedit/$',views.hostedit),

view.py:

def hostedit(request):

if request.method == 'POST':
    editForm = addhostForm(request.POST)

    if editForm.is_valid():
        addTime = editForm.save(commit=False)
        addTime.ExpirTime = '2015-10-10'
        # addTime.save()
        return render_to_response('staticfiles/lnasset/hostedit.html',{'seccus': '成功',
                                                                            'a':editForm,
                                                        },context_instance=RequestContext(request))
    else:
        return render_to_response('staticfiles/lnasset/hostedit.html',{'seccus': '失败',
                                                                              'a':editForm,
                                                        },context_instance=RequestContext(request))
else:
    query = request.GET.get('id')
    results = ServerInformation.objects.filter(id = query)
    return render_to_response('staticfiles/lnasset/hostedit.html',{
                                                            'results':results,
    },context_instance=RequestContext(request))

这里if中的POST和GET。分别,POST是编辑页是“提交”的POST请求。GET是点击“编辑”时。 我现在的问题是。我提交时,每次数据库都插入了一条新的数据,不是更新。这里我也理解因为这里使用了一个addhostForm. 我想请问 怎么实现我点击编辑之后,是更新这条数据。而不是插入一条新的数据。如何做啊。越学越头大。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/1348
 
2335 次点击  
文章 [ 8 ]  |  最新文章 8 年前
415293799
Reply   •   1 楼
415293799    8 年前

@三十撸啊撸 嗯。多谢。我试试。因为前端很多不懂。很多都是去看别人的代码。

三十撸啊撸
Reply   •   2 楼
三十撸啊撸    8 年前

将你的代码按如下修改

request.GET时

results = ServerInformation.objects.filter(id = query)改为

results= ServerInformation.objects.get(id = query)

#因为filter返回是一个list,不是单个实例

在你的hostedit.html的form里加入

<form>

...

<input type='text' name="id" value="{{ results.id }}" hidden="true"/>

...

</form>

#在form中放置一个input成员,value为results.id并将其隐藏,form在提交时,就能够从POST中取到id了

415293799
Reply   •   3 楼
415293799    8 年前

@三十撸啊撸 query = request.GET.get('id')使用这个时,是要前端get请求吧,我“编辑”是get请求,编辑页面的“提交”POST请求,这样还可以获取到id吗?我去试试,越来越纠结了。多谢你的回答。

415293799
Reply   •   4 楼
415293799    8 年前

@shen_gan addhostForm是添加时的from表。我编辑的时候也想到了用这个表。你说的save添加一个参数,我还是没明白。这个save是在“添加”功能时加参数,还是“编辑”功能时加参数?不过我得好好看看save.

415293799
Reply   •   5 楼
415293799    8 年前

@fc不将就 多谢,找了很久的文档。

三十撸啊撸
Reply   •   6 楼
三十撸啊撸    8 年前

@415293799 在创建editForm时传入要修改的实例即可,这样editForm在save时执行的时update操作

# editForm = addhostForm(request.POST)

query = request.GET.get('id')

a = ServerInformation.objects.get(id = query)

editForm = addhostForm(request.POST, instance=a)

shen_gan
Reply   •   7 楼
shen_gan    8 年前

addhostForm 应该是添加和编辑时的共同表单吧。
既然是更新,则也是需要先知道更新对象的信息,也就是说,也需要根据传入的 id 获取数据。然后对此 id 对象更新即可。

其中一个具体操作可以是:

  1. 在 addhostForm 的 save 函数添加一个参数,此参数是需要更新的对象(默认值为 None)
  2. 在 save 函数里面,则是根据更新对象的值是否为 None,对应新添(为 None 时)还是更新(不为 None 时)
fc不将就
Reply   •   8 楼
fc不将就    8 年前

from django.views import generic

class BlogUpdate(generic.UpdateView):

       model = ServerInformation
       fields = ["ExpirTime(要更新的字段)","要更新的字段"]
       template_name_suffix = '_update_form'[模版名称]

参考:http://python.usyiyi.cn/django/ref/class-based-views/generic-editing.html#updateview