Skip to content
Related Articles

Related Articles

Associate user to its upload (post) in Django
  • Difficulty Level : Basic
  • Last Updated : 27 Feb, 2020

Django provides built-in facilities of ForeignKey, ManytoManyField for associating one model to other 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 application of post upload and also profile app which contains all the past uploads of the user.

Prerequisites –

  1. Creation of Django project
  2. Creation of application which can register login and logout the user
  3. 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 as userblog (blog upload from the user) .To do this create an app in 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 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 makemigrations
python 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 file of userblog application which is named as Snippet. We will use a ForeignKey class which will hold the id value of the user and it holds one to many relationship and so you can use this class to associate the user to any other activities where there is user involvement.

from django.db import models
from django.conf import settings
User = settings.AUTH_USER_MODEL
# Create your models here.
class Snippet(models.Model):
    user = models.ForeignKey(User,
                        default = 1,
                        null = True
                        on_delete = models.SET_NULL
    blogname = models.CharField(max_length = 100)
    blogauth = models.CharField(max_length = 100)
    blogdes = models.TextField(max_length = 400)
    img = models.ImageField(upload_to ='pics')
    def __str__(self):
        return self.blogname

Also create a python file named as and create a ModelForm for the same to input data from user.

from django import forms
from .models import Snippet
class SnippetForm(forms.ModelForm):
    class Meta:
        model = Snippet
        fields = ['blogname',

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 the class Snippet and you will see this in django administration –
Here User is a foreign key which will show all the users and it will store the key number of last instance of post upload by a user by default it is set to superuser
Now we will go to file of application and add the main code which will be storing the information in 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 ad it will associate blog to user

from django.shortcuts import render
from django.http import HttpResponse
from .forms import SnippetForm
from .models import Snippet
from django.contrib import messages
# Create your views here.
def usblog(request):
    snipps = Snippet.objects.all()
    return render(request, 'indexg.html', {'snipps' : snipps})
def snippet_detail(request):
    form = SnippetForm(request.POST or None, request.FILES or None)
    if request.method =='POST':
        if form.is_valid():
            obj = = False)
            obj.user = request.user;
            form = SnippetForm()
            messages.success(request, "Successfully created")
    return render(request, 'form.html', {'form':form})

So by now the Django administration has created the database of Snippet class and you can see it by visiting Django administration. Now we have to create a simple form.html file which we will contain a form from where user can enter the queries which we have stated in the class. Here comes the beauty of Django that since we have used model forms for our application Django has created HTML code of form which will have all those queries which we needed. So simply create an HTML file in your templates file(form.html).

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Your Blog</title>
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <button type="submit">Submit</button>

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 file. (Placed image of only body part of html to show the python code you can make your own indexg with features )

    <h1>Post play<h4>Only for geeks</h4>
    <div class="topnav">
        {% if user.is_authenticated %}
        <a href="#">Hi {{user.username}}</a>
        <a href="accounts/logout">Logout</a>
        <a href="snippet_detail">Write post</a>
        {% else %}
        <a href="accounts/register">Register</a>
        <a href="accounts/login">Login</a>
        {% endif %}
        {% for snips in snipps %}
        <img src="{{snips.img.url}}" alt="Image" class="img-fluid">
        <h2 class="font-size-regular"><a href="#">{{snips.blogname}}</a></h2>
        {% if user.is_authenticated %}
        {% else %}
        <p>Register/Login to know more</p>
        {% endif %}
        {% endfor %}

Let us go to our main urls file where we will have an account app and now make the userblog application as default and add the URLs of your application. Also in your userblog application add and add the links of 2 functions which are form.html and homepage(indexg.html).
Main Urls

from django.contrib import admin
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
   path('', include('userblog.urls')),
    path('accounts/', include('accounts.urls'))

userblog urls –

from django.urls import path
from . import views
urlpatterns = [
    path("snippet_detail", views.snippet_detail),
    path('', views.usblog, name ='usblog')

Start the application and register the user to your application and make a post

python runserver

Your browser does not support playing video

GITHUB LINK – Github Repo

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :