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



Python


In this article, we show how to create a create view with 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 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 a Django 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 create the ModelForm.

forms.py File

So now we have to create the forms.py file.

This contains the ModelForm for the Create Post.

This is shown below.





So the ModelForm is very basic.

We have to import the Createpost model we created in the models.py file.

The ModelForm we will create is the Createform ModelForm.

We include only the title and content that the user can see and needs to fill out.

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/

views.py File

Next, 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.

We then create the variable, form, and set it equal to Createform(request.POST or None)

We don't save the form at first because we need to enter the author of the post, which is, request.user

We then save the post objecdt.

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

We then render the template file.

Else, we return an error message that the post was not successfully created.

Template File

Lastly, 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 Django form.

This template file will be called create.html.

The create.html template file is shown below.



So we simply create the form.

We set the action attribute to "", because we want it to stay on this page.

And we set the method attribute to "POST", because we want the data posted to the screen and not to the URL (which would be the case with the GET method).

We then must include {% csrf_token %}, as a security measure.

We then include the Django form on the page with the line, {{ form.as_p }}

We then add the submit button to the form before we close it.

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


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...