开发者

Django using a generic view for create_update update_object, form not displaying

开发者 https://www.devze.com 2022-12-13 03:23 出处:网络
Trying to use a generic view so I can update an object via a user facing form. My code looks like this开发者_开发百科 in views:

Trying to use a generic view so I can update an object via a user facing form. My code looks like this开发者_开发百科 in views:

from django.views.generic.create_update import update_object

@permission_required('myapp.change_foo', login_url="/accounts/login/")
def foo_update(request, foo_id):
    return update_object(
        request,
        form_class=FooForm,
        object_id=sr_id,
        template_name = 'foo/update.html',
        template_object_name = 'foo',
    )

The form definition looks like this:

class FooForm(ModelForm):
    somefield = forms.CharField(
        widget=forms.TextInput(attrs={'readonly':'readonly'})
    )

    class Meta:
        model = Foo

And in my template I have a snippet like this in foo/update.html:

    <form action="" method="post"> 
        <table>
            {{ foo.as_table }}
        </table>
        <p>
            <input type="submit" value="Submit" />
        </p>
    </form>

I've never had issues displaying 'foo' before, but this is the first time I'm using this particular generic view. No syntax errors, just no form showing up in my template at all.

Does update_object() not embed 'foo' as a form? is it still up to me to add the form add additional content and then write the save() logic for it? I wish there was slightly more documentation on this generic view and how it works.


It's right there in the documentation:

In addition to extra_context, the template's context will be:

  • form: A django.forms.ModelForm instance representing the form for editing the object. This lets you refer to form fields easily in the template system.

The template_object_name argument influences the name of the actual object.

/edit

Jeez, you people... criticizing one of the best documented open source projects out there, but not even trying to read the documentation if it is spoonfed to you. To continue my quote of the docs:

For example, if the model has two fields, name and address:

 <form action="" method="post">
 <p>{{ form.name.label_tag }} {{ form.name }}</p>
 <p>{{ form.address.label_tag }} {{ form.address }}</p>
 </form>

See the forms documentation for more information about using Form objects in templates.

0

精彩评论

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

关注公众号