How to Create a Website that Allows for User-Generated Posts with Python in Django



Python


In this article, we show how to create a website that allows for user-generated posts with Python in Django.

So what is meant by this is how can we create a website that allows users to create user-generated pages.

This is what websites such as quora or stackoverflow does.

A user can ask a question or post an article with a form and upon submission, a complete webpage is created off of the user's form. Each page that is user-generated that has a comment box where other users can add comments; however, we will just keep it simple for this page.

We'll have a form where a user can type in a question just like stackoverflow, for example, and upon submission of the form, a complete other webpage will be created, with the standard template for the website, with the question the user asked.

So let's go ahead and do that.


models.py File

So the first thing we'll have to do is create the database table.

We'll create a simple database that consists of the bare minimum: user (the user who submitted the comment), title (the title of the post), content (the content of the post), and the date published.

I can't think of anything else.

Again, obviously, on a website like stackoverflow or quora, each page will have a commentbox where users can add comments and reply to each other's comments, but we're keeping it simple here. We're just going to show how to have a user have a post and then have it make this post show up on its own page.

The code for this database table is shown below.



So we have created a database table called UserPost that has the fields: user, title, content, date_published, and url.

We then create a save() function underneath this database table definition.

This allows us to save the url as the title.

So let's go through each part with more in-depth analysis.

So we have a database table called UserPost.

Because we want the user associated with the post, we create a ForeignKey that links each post on the website with the user who created it.

We then have a title, because we want each post to have a title. A title is a requirement for each post.

We then have a content textfield, where the user can write the post.

We then have the field, date_published. This shows the date when the post was created. You probably also want a database field, last_updated, to show on the page when the post was last changed. But this is just a simple example, so we'll leave it out in this case.

Next is a very important field, almost the heart of our code, because we generate a URL, which will act as the URL that the page will exist on.

The url field will have the same name as the title field, but slugified.

You want to slugify urls. Because you don't want URLs to have spaces in them; this is because web browsers add %20 to spaces, which makes the URL look ugly and not very human readable.

Therefore, we write in the save() function, so that the title is slugified.

This concludes the models.py File.


forms.py File

Now that we have created the models.py file, we now have to create the actual form where a user enters in a post.

To do this, we simply create a modelForm. A modelForm is a form that is based off a model (a database table).

We create this form through the following code, shown below.

So now we this code, we have created a form named UserPost based off of the UserPost model (database table).

The only fields that we need to display are the title and content fields.

This concludes the forms.py file.


views.py File

The next thing we want to do is go to views.py file.

In this file, we create the create view, the list view, and the detail view for the page.

The create view is where the user can create the post.

The list view is the list of all posts.

The detail view is the separate page of the post by itself. This will be indexed according to the URL.

The code for all of this is shown below.



So in this views.py file, we just create the various function-based views that we use in our code.

We have a function for the create view, the list view, and the detail view.


urls.py File

Now let's go to the urls.py file.

In this file, we specify the urls for all pages.



So in this urls.py file, we have to import the functions we created from the views.py file.

In the urlpatterns list, we specify all the urls.

If the user types in, http://127.0.0.1:8000/Blog/create/, s/he goes to the create page, where s/he can create a post.

If the user types in, http://127.0.0.1:8000/Blog/posts/, s/he goes to the list view of the blog posts.

If the user types in, http://127.0.0.1:8000/Blog/posts/url, s/he goes to the detail page of the specific post where url is the slug name of the title of the post.

r'^posts/(?P\S+)/$' accepts any type of characters for the URL. This is a regular expression statement. You want to put this after URL because it can literally have any character, except for spaces, since we've slugified the title. It can have alphabetical characters, numbers, etc.


create view

The contents of the create view template file, userposts-create-view.html, is shown below.



In this create view, we simply put the modelForm we created.


List view

The contents of the list view template file, userposts-list-view.html, is shown below.



In this list view, we simply list all the posts in the database table.

At the bottom of the page, we hardcore the detail view of the url that we create.

We could also specify this url by the following line, <a href='{% url "blog:userposts_detail_view" post.url %}'>{{ post.title }} </a>


Detail View

Lastly, we have the detail view template file, userposts-detail-view.html, which is shown below.



These are just very basic template files.

This brings us to the detail view, where we are able to both view the title and the content of the post.

And this is all there is to creating a website that allows user-generated posts with Python in Django.

As stated before, this article was written just to show you the basic mechanics of how to set up these pages and encode the slugified title of the post in the URL.

You can get much more advanced than this, where you create a comment section in the detail view of the post so that users can comment.

But, again, this is just the basics.


Related Resources

How to Insert Images into a Database Table with Python in Django

How to Insert Files into a Database Table with Python in Django

How to Insert Videos into a Database Table with Python in Django



HTML Comment Box is loading comments...