How to Create a Create View with an HTML Form in Django



Python


In this article, we show how to create a create view with 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 a create view, which is one part of CRUD.

With a create view, a user can create a post (such as with quora or stackoverflow), upload a video (as with youtube), upload images (as with pinterest), etc.

So a create view is kind of like the first step of CRUD. Without being able to create a post, then there is no retrieval, updating, or deleting the post.

So let's go over how to create a create view with an HTML form in Django.

models.py File

So the first thing that we have to do is create the database (model) for the create view that we're going to make.

This is because we're going to save the post that the user creates to the database.

We will create a simple form where we just have 3 fields, title, content, and author.

We will call this model, Post.

This is shown below.



So this is basic model.

The model has 3 fields: title, content, and author.

We create a __str__ method just to make the objects in this table more readable (instead of just display the generic object).

Next, we go to the urls.py file.

urls.py File

So now we go to the urls.py file, because we need a URL for the create page.

This URL will be, /posts/create/

So we now go to the urls.py file in the posts app to create this URL.

The urls.py file will look like the page below.



So we've now created the URL, where a user can create a post.

The URL once again is, /posts/create/

Template File

Next we go to the template file, which contains the create form, where a user can create a new post.

Again, this is going to be a form written purely in HTML.

This template file will be called create.html.

The create.html template file is shown below.



So we just code our HTML form using the <form> tag. This form tag has an action and a method attribute. The action attribute is set to an empty string, because we want the data on this page. We make the method, POST, because we want the data posted to the page rather than to the URL.

We then create a text input with a name of "title"

We then create a textarea with a name of "content"

As with all pure HTML forms, a submit button is needed. We give the submit button a value of "POST"

This ends the template file.

views.py File

Lastly, we have our views.py file.

There is where we take the data that the user has entered in and put in the database.

The views.py file is shown below.



So we create the function, createpost.

Because only users can create posts, we put the login_required decorator above the function.

If the method is POST, then we check to see if the title nad content form fields have been filled in.

If they have, we create a variable named post and set it equal to the Post model with the line, post=Post()

We then take this variable post and can use it to set all the attributes within the database table.

We set the title attribute of the post using the line, post.title= request.POST.get('title')

We set the content attribute of the post using the line, post.content= request.POST.get('content')

We the set author attribute of the post using the line, post.author= request.user

We then save the database values using the line, post.save()

We create a message of success by using the line, messages.success(request, "Your post has been successfully created")

We then return whatever template file we want.

Else, the creation of the post was unsuccessful and we send the user an error message, alerting the user to this.

And this is how we can create a create view with an HTML form in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...