How to Save The Current User Logged In to a Database Table in Django



Python


In this article, we show how to save the current user logged in to a database table in Django.

If you're creating any type of serious website, especially that contains user-generated content, then you need to know which user has submitted what content.

That way, everything that exists on the site is linked to a user, so we can know which user uploaded what content or wrote what content (such as comment).

This is important because then you organize each person's content on the site.

If a user named kyrie uploaded 3 videos and wrote 2 comments, you can pull of this information just based on the user.

Also, if a user deletes his/her account, you have the option of deleting everything that the user uploaded or wrote on the site.

So if you're creating any type of site that allows for user-generated contents, such as image uploads, file uploads, comments, likes (such as for videos such as on youtube or for comments), then every database that you have for allows for user content should have a User field.

This is what we can link every type of content on the site to a user.

So let's show how to do this now. We'll be doing this chiefly in the views.py file.

models.py File

We will create a database table called Video that has 4 fields: user, name, description, and videofile.

This is shown in the code below.



So you can it's a simple model (database table)>

To create the user database field, we use Django's built-in User model, not our custom Model.


views.py File

Now let's go over the code how to save the current user that is logged into the user database field.

In order to do this, we have to do some manipulations to the form data.

This is shown in the code below.



So this is our views.py file.

We import the Video database table, since we'll be passing it into the template file.

We then import the form that we created which is the a ModelForm of the database table, Video, so it's just the form replication of the database table, so it isn't necessary to see it (same fields).

We create a variable, allvideos, to get all objects (videos) from the Video database table.

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

This is because it's a form that accepts text data and file data (file upload).

We then check to see if the form data is valid through the line, form.is_valid()

We then create a variable fs (you can name it anything but I named it fs for file save).

We set fs equal to, form.save(commit=False)

This doesn't save the form data immediately.

We then reference the user database field through the statement, fs.user

We set fs.user= request.user

What this line does is, it sets fs.user equal to an instance of the Django User Model. request.user refers to an instance of a User in Django's built-in User Model. And because it's request.user, it is referring to the user that is currently logged in and who submitted the form.

If you set the user field equal to anything other than an instance of the User Model, then you will get an error.

For example, if you set the user field equal to, request.user.username, an error will be thrown. This is because request.user.username is the username of the user, not an instance of a user.

So be careful with that. A user field from Django's built-in User model can only have an instance of a user entered in.

We then save the changes of the form we have made using the line, fs.save()

We then pass in the form and allvideos variables into the context dictionary and then pass the context dictionary into the template file for rendering.


Alternative Method

Alternatively, you could add a created_by database field to the table.

So we can add the following line, shown below, to the database table.



And then in the views.py file, we can set the user by the following line shown below.



Or the following line would work as well.

fs.created_by= request.user

Thus, now you have a created_by field that stores who created the content.

But the above code works just as well. So this is unnecessary, but just to show you another way how to do it.

And this is how we can save the current user logged in to a database table in Django.


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