Understanding the Flow of Django

Dear Django enthusiast,

You may be learning Django through watching YouTube videos, reading books or blogs. You see the instructors switching from models.py, views.py, urls.py, templates, settings.py and you are left wondering... How and when do I know when to move to a different file or create a certain file? Yes, it can get frustrating.

Today we will try and understand the flow of Django, what is the approach to take?

mvt.png

Django uses the MVT design, meaning: Model, View, Template. The Model helps handle the database, the Template is where data and design is presented also know as the User interface and the View handles all the business logic. it interacts with the model, gets data and renders it to the template and vice versa. The view is the intermediary between the Model and Template. Django basically acts as a controller.

Therefore, whenever you are starting a project remember this MVT design.

Django MVT ILLUSTRATION

Lets take a blog example,

First create an Article Model for your blog in the models.py file. The model will create the database table with fields such as title, slug, body, date that will enable authors to create various articles and publish them.

class Article(models.Model):
    title = models.CharField(max_length=100)
    slug = models.SlugField(null=False,unique=True)
    body = models.TextField(max_length=250)
    date = models.DateField(auto_now_add=True)
    thumb = models.ImageField(default='default.jpg',blank=True)
    author = models.ForeignKey(User,default=None,on_delete=models.CASCADE)

    def __str__(self):
        return self.title

Second, create a view for your model. Here, you create the blogs logic, giving each and every field in the model a particular logic. For example:

  • How will the articles be arranged on the blog? Should they be ordered by date, author or...?

  • What will happen when someone searches for a particular article? will querying happen by date, author, article title or...?

  • What will happen if someone wants to create a blog post, should they first login?

  • How will the program know that the login and signup details entered by the User are valid so that it can save to the database?

def article_create(request):
    if request.method == 'POST':
        form = forms.CreateArticle(request.POST,request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.author = request.user
            instance.save()
            return redirect('articles:list')

    else:
        form =forms.CreateArticle()
    return render(request,'articles/article_create.html',{'form':form})

Third, create the template for people to view your design. Here, design the webpage UI, taking into account how the articles will be displayed, how article comments will be displayed, how the signup and login pages will look like...etc.

Last but not least, we have the urls.py. Here, you provide routing for each and every view function created in the views.py file and render the specific template based on the logic criteria.

Summary:

From the Model to the View to the template and then the url

A user requests for a resource in Django which works as a controller, and checks if the resource is available in the URL.

If URL is available, a view is called that interacts with the model and template, and renders a specific template.

Django responds back to the user and sends a template as a response.

Hope this helps, as always Happy coding and stay safe 🥂