Associate user to its upload (post) in Django
Django provides built-in facilities of ForeignKey, and ManytoManyField for associating one model to another model. To associate a user model to a post model, one can use various options. This article revolves around how to associate a user to its post (post model). This tutorial uses the concept of foreign keys in Django and at the end, one will be able to create an application of post upload and also a profile app which contains all the past uploads of the user.
- Creation of Django project
- Creation of application which can register login and logout the user
- Make migrations in application and add database
We have already created a user application for registration and so we will create a new app that can be named a user blog (blog upload from the user) .To do this create an app in the main project file by writing this code in your PowerShell or terminal
django-admin startapp userblog
Now this application is available in your project file and you should first add this application to the settings.py file in the project and add this application to INSTALLED_APPS
Now make migrations in your project and add this application to your project
python manage.py makemigrations python manage.py migrate
Now we have to use models in this application so that Django can create a table for the information which we are going to store in our database and the user can input the information. We have to create a class in the models.py file of userblog application which is named Snippet. We will use a ForeignKey class which will hold the id value of the user and it holds too many relationships and so you can use this class to associate the user to any other activities where there is user involvement.
Also, create a Python file named forms.py and create a ModelForm for the same to input data from the user.
We need to migrate the class model of Snippet so that Django administration creates a database for the post upload and details so makemigrations of class Snippet and you will see this in django administration –
Here User is a foreign key that will show all the users and it will store the key number of the last instance of post upload by a user by default it is set to superuser Now we will go to the views.py file of the application and add the main code which will be storing the information in a database using model form object.
- usblog – This will display all the posts in our homepage
- snippet_detail – This will get the data from the user in the form and it will associate blog to user
So by now, the Django administration has created the database of the Snippet class and you can see it by visiting Django administration. Now we have to create a simple form.html file that will contain a form from where the user can enter the queries which we have stated in the class. Here comes the beauty of Django since we have used model forms for our application Django has created an HTML code of form that will have all those queries which we needed. So simply create an HTML file in your templates file(form.html).
Now we will need a homepage where we will see all the posts of the users so create another HTML file indexg.html and import the objects of function which we have created in views.py file. (Placed image of the only body part of HTML to show the python code you can make your own indexg with features )
Let us go to our main urls file where we will have an accounting app and now make the userblog application as default and add the URLs of your application. Also in your userblog application add urls.py and add the links of 2 functions which are form.html and homepage(indexg.html).
userblog urls –
Start the application and register the user to your application and make a post
python manage.py runserver
Your browser does not support playing video GITHUB LINK – Github Repo
Please Login to comment...