开发者

Django: Calling ModelForm via instance method - seperate fields

Is it possible to output a ModelForm using a class method; for example:

def edit_form(self, *args, **kwargs):
    from smf.node.forms import MediaBaseForm
    return MediaBaseForm(instance=self)

(MediaBaseForm is a ModelForm subclass for model Media), and then in the view:

form = node.edit_form()
(node contains the instance)

This code executes without raising errors; however, when viewing the page, no form fields are generated when I try to render each field separatly. The form does display when I use {{ formset }} but not using {% for form in formset.forms %}} ..etc. {% endfor %} -->

I need this:

{{ formset.management_form }}
    {% for form in formset.forms %}

        {{ form.id }}
        <div id="form_title">Title: {{ form.title }}</div>
        <p>Description:</p> {{ form.description }}
        <p>Comment settings:</p> {{ form.comment }}
        <p>Promoted to frontpage:</p> {{ form.promote }}
        <p>Sticky:</p> {{ form.sticky }}
     开发者_开发知识库   <p>Ready for Publishing:</p> {{ form.status }}
    {% endfor %}

instead of this:

{{ formset }}    

Is it not possible to call a form this way or am i doing something wrong?

Complete view:

def node_edit(request, nid):
        #Build the standard node form
        node = node_load(nid)
        obj = node_get_model(node.type, True)
        #modelFormset = modelformset_factory(obj, max_num=1)
        form = node.edit_form()

    if request.method == 'POST': # If the form has been submitted...
        #form = modelFormset(request.POST, request.FILES)
        form = node.edit_form()
        if form.is_valid():
            instances = form.save(commit=False)
            for instance in instances:
                instance.save()
            node_post_process(node, request, obj)

            if 'save_edit' in request.POST:
                id = int(nid)
                return HttpResponseRedirect('/edit/%i' %(id))
            if 'save' in request.POST:
                #id = node_callback_value.Node_ptr
                id = int(nid)
                return HttpResponseRedirect('/content/%i' %(id))
            if 'savenew' in request.POST:
                return HttpResponseRedirect('/content/add/%s' %(node.type))                
    else:
        output = {}
        #form = modelFormset(queryset=obj.objects.filter(node_ptr=nid))
        form = node.edit_form(instance=node)
    output = {
    'formset': form,
    'node': node,
    }
    try:
        return render_to_response('node/forms/%s.edit.html' %(node.type), output)
    except TemplateDoesNotExist:
        return render_to_response('node/forms/default.form.html', {
            'formset': form,
        })     


I can't understand what you're trying to do. There are a number of problems with your approach, though.

Firstly, there's no way of passing the POSTed values to the form instantiation. As a result, form.is_valid() will always be False.

Secondly, the result of node.edit_form() appears to be a single form, not a formset. So I don't know why you are passing it to the template context as formset and trying to iterate through formset.forms.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜