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


In this article, we show how to create an update view of an HTML 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 was originally written in HTML, as opposed to a Django form.

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

So let's say we have created the following HTML form in the template file that renders our HTML form.

It's a very simple form, consisting of just title and content fields for the user to create a post.

The user now creates the post and is brought to the detail view of the post after the creation.

How can we now allow the user to edit the post with an update view?

There's a number of steps that must occur, so let's go each each step one at a time. 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 database, 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.

If the method used is a POST method, then we get the title and content of the post.

We then get the specific post and use the update() function to set the title and the content of the post.

We then create a message of success that the post was successfully created.

We then create a context dictionary, in which we pass in the post object. We need to do this to retrieve its contents.

We then render out the template we want. It is your choice which page you want to return out. It may be that you want to serve up back the update page after it's successfully been edited, or you may want to do a redirect to the detail page.

Lastly, we just need an else statement if the update was not successful. In this case, it makes the most sense to return the update page, so that the user can try again.

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, but now with the values inside of the forms.

We put the values inside of the form by passing in the postobj and then specifying the respective attributes in the respective form fields.

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

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...