基于添加表单的Django编辑表单? [英] Django edit form based on add form?

查看:159
本文介绍了基于添加表单的Django编辑表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个很好的形式,还有一个很复杂的'add'功能来处理它。它开始像这样...

  def add(req):
如果req.method =='POST' :
form = ArticleForm(req.POST)
如果form.is_valid():
article = form.save(commit = False)
article.author = req.user
#more processing ...

现在我真的不想复制所有的功能在 edit()方法中,所以我想到编辑可以使用完全相同的模板,也可能只是添加一个 id 字段到窗体,所以添加函数知道它是什么编辑。但是这个问题有一些问题。


  1. 我将在哪里设置 article.id 添加 func?它必须在 form.save 之后,因为这是文章创建的地方,但它永远不会达到这一点,因为由于唯一约束,表单无效(除非用户编辑所有内容)。我可以删除 is_valid 检查,但是后来 form.save 失败。

  2. 如果表单实际 无效,则动态添加到编辑功能中的字段不会被保留。

那么我该如何处理?

解决方案

如果你是从ModelForm扩展你的表单, code>实例关键字参数。在这里,我们传递现有的实例或新的,具体取决于我们正在编辑或添加现有文章。在这两种情况下,作者字段在实例上设置,因此不需要 commit = False 。还要注意,我假设只有作者可能编辑自己的文章,因此HttpResponseForbidden响应。

  from django.http import HttpResponseForbidden 
from django.shortcuts import get_object_or_404,redirect,render,reverse


@login_required
def edit(request,id = None,template_name ='article_edit_template.html '):
如果id:
article = get_object_or_404(article,pk = id)
如果article.author!= request.user:
返回HttpResponseForbidden()
否则:
article = Article(author = request.user)

form = ArticleForm(request.POST或None,instance = article)
如果request.POST和form.is_valid ():
form.save()

#保存成功,所以重定向到另一个页面
redirect_url = reverse(article_save_success)
return redirect(redirect_url)

return render(request,template_name,{
'form':form
})

在你的网址.py

 (r'^ article / new / $',views.edit, },'article_new'),
(r'^ article / edit /(?P< id> \d +)/ $',views.edit,{},'article_edit'),

同样的编辑视图用于添加和编辑,但是只有编辑URL格式将id传递给视图。为了使您的表单正常工作,您需要从以下格式省略作者字段:

  class ArticleForm(forms.ModelForm):
class Meta:
model = article
exclude =('author',)


I've made a nice form, and a big complicated 'add' function for handling it. It starts like this...

def add(req):
    if req.method == 'POST':
        form = ArticleForm(req.POST)
        if form.is_valid():
            article = form.save(commit=False)
            article.author = req.user
            # more processing ...

Now I don't really want to duplicate all that functionality in the edit() method, so I figured edit could use the exact same template, and maybe just add an id field to the form so the add function knew what it was editing. But there's a couple problems with this

  1. Where would I set article.id in the add func? It would have to be after form.save because that's where the article gets created, but it would never even reach that, because the form is invalid due to unique constraints (unless the user edited everything). I can just remove the is_valid check, but then form.save fails instead.
  2. If the form actually is invalid, the field I dynamically added in the edit function isn't preserved.

So how do I deal with this?

解决方案

If you are extending your form from a ModelForm, use the instance keyword argument. Here we pass either an existing instance or a new one, depending on whether we're editing or adding an existing article. In both cases the author field is set on the instance, so commit=False is not required. Note also that I'm assuming only the author may edit their own articles, hence the HttpResponseForbidden response.

from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404, redirect, render, reverse


@login_required
def edit(request, id=None, template_name='article_edit_template.html'):
    if id:
        article = get_object_or_404(Article, pk=id)
        if article.author != request.user:
            return HttpResponseForbidden()
    else:
        article = Article(author=request.user)

    form = ArticleForm(request.POST or None, instance=article)
    if request.POST and form.is_valid():
        form.save()

        # Save was successful, so redirect to another page
        redirect_url = reverse(article_save_success)
        return redirect(redirect_url)

    return render(request, template_name, {
        'form': form
    })

And in your urls.py:

(r'^article/new/$', views.edit, {}, 'article_new'),
(r'^article/edit/(?P<id>\d+)/$', views.edit, {}, 'article_edit'),

The same edit view is used for both adds and edits, but only the edit url pattern passes an id to the view. To make this work well with your form you'll need to omit the author field from the form:

class ArticleForm(forms.ModelForm):
    class Meta:
        model = Article
        exclude = ('author',)

这篇关于基于添加表单的Django编辑表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆