Django edit form based on add form?

后端 未结 2 626
渐次进展
渐次进展 2020-11-28 01:44

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\':
        f         


        
2条回答
  •  误落风尘
    2020-11-28 02:38

    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\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',)
    

提交回复
热议问题