How to Create an Update View of a Django Form in Django


In this article, we show how to create an update view of a Django form in Django.

CRUD is a very important concept in website design.

CRUD stands for Create, Retrieve, Update, and Delete.

With CRUD implemented, a user can create a post, retrieve the post, update the post, and delete the post.

In this article, we will show how to create an update view, which is one part of CRUD.

With an update view, if the user created the post, the user can edit the post.

This is seen on almost all sites. If you go on, if you post a comment, you can edit the comment. The same thing is the case for many sites.

Specifically, in this article, we show how to create an update view when the form is a Django form, as opposed to an HTML form.

The process is a little different for Django forms vs. HTML forms. We hone in on Django forms in this article.

So let's say we have created the following Django model in the file in our app.

So we create a model, Createpost, that has 3 form fields: title, content, and author.

We create a __str__ method just to make the objects more readable in the admin database.

Next, we create a ModelForm of this model, which has the code shown below. This is in the file.

When showing the form to the user, we only display the title and content fields.

Now we can move onto the file. File

Let's start with the File.

We must create a URL for the update page.

Let's say the URL will be, /posts/postid/edit, where postid is the id of the post.

This is what the file will look like.

So since this is the posts app, this URL will form the URL< /posts/postid/edit/, where postid is the id of the post. In the file in the root directory, we listed that all the posts here are preceded by /posts/. This is why there is a preceding /posts/ in the url.

When this URL is entered in, the file will execute the editpost function. File

Now let's go to the file.

This is where we have the functionality for the update view.

Our file will contain the following.

So we have to import a number of things. We import render, get_object_or_404, login_required, the Post model, the Createform form, and messages.

Since only an authenticated user can edit a post, we add a login_required decorator to the editpost function.

In the definition of the editpost function, we have 2 parameters: request and id.

We are updating a specific post, so we need to uniquely identify that post with the id attribute (which is the primary key for the post objects).

We then retrieve the post through the get_object_or_404() function. We pass into this function the Post database and the id attribute, so that the Post object can be uniquely identified.

We create a variable, form, and set it equal to Createform(request.POST or None, instance= obj)

Createform is the ModelForm we are using in this case. We pass in request.POST or None, so that the form retains its values if any has been filled in. We pass in, instance= obj, so that we pass in the specific instance we want brought up. In this case, it's the Post object so that contains the id in the URL we created.

If the form is valid, we save the form, meaning we save the new changes.

We then create a message of success that the form was successfully updated.

We then pass in the form variable into the context dictionary and return the template.

If not successful, we send an error message, alerting the user to this.

Lastly, we need the update template file.

Update Template File

Lastly, for this project, we need the update template file.

For this example, I call my template file, edit.html

Its contents will resemble the following shown below.

So we just have the same HTML form that we had in the Create view, except now you just have the button with the value of 'Edit Post' instead of Create Post.

And this is all that is needed to create an update view of a Django form in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...