DEV Community

Cover image for Understanding Django Fundamentals
kihuni
kihuni

Posted on

Understanding Django Fundamentals

In the previous blog, we set up our development environment and created a basic Django project and app. Now, it's time to dive deeper into the foundational aspects of Django, including its project structure, Model-View-Template (MVT) architecture, and the Django admin interface. By the end of this post, you should have a solid understanding of these concepts and be ready to create a simple blog application.

Overview

This blog will delve into the foundational aspects of Django, including its project structure, MVT architecture, and the Django admin interface.

Topics Covered

  • Django Project Structure
  • Models, Views, and Templates (MVT)
  • Django Admin

Objectives

  • Understand the MVT architecture
  • Create models, views, and templates in Django
  • Use the Django admin interface

Django Project Structure

Understanding Django's project structure is crucial for navigating and organizing your code effectively. When you create a new Django project and app, the following directory structure is generated:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        wsgi.py
        asgi.py
    blog/
        __init__.py
        admin.py
        apps.py
        models.py
        tests.py
        views.py
        migrations/

Enter fullscreen mode Exit fullscreen mode
  • manage.py: A command-line utility that helps manage the Django project.
  • myproject/: The main project directory containing settings and configuration.
  • settings.py: Configuration settings for the project.
  • urls.py: URL declarations for the project.
  • wsgi.py and asgi.py: Entry points for WSGI/ASGI-compatible web servers.
  • blog/: A Django app directory containing application-specific files.

Models, Views, and Templates (MVT)

Django follows the Model-View-Template (MVT) architecture, which is a variation of the MVC pattern. This architecture promotes a clean separation of concerns, making your code more organized and maintainable.

Models

Models define the structure of your database tables. Each model is a Python class that subclasses django.db.models.Model.

# blog/models.py

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=100)
    content = models.TextField()
    published_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

Enter fullscreen mode Exit fullscreen mode

Views

Views handle the logic and data processing for your application. They take requests, interact with models, and return responses.

# blog/views.py

from django.shortcuts import render
from .models import Post

def home(request):
    posts = Post.objects.all()
    return render(request, 'blog/home.html', {'posts': posts})

Enter fullscreen mode Exit fullscreen mode

Templates

Templates define the HTML structure and presentation of your web pages. They can include dynamic content by using Django template tags and filters.

<!-- blog/templates/blog/home.html -->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Blog Home</title>
</head>
<body>
    <h1>Blog Posts</h1>
    {% for post in posts %}
        <h2>{{ post.title }}</h2>
        <p>{{ post.content }}</p>
        <p>Published on: {{ post.published_date }}</p>
    {% endfor %}
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

URL Configuration

To map URLs to views, the URL patterns need to be configured in urls.py.

# myproject/urls.py

from django.contrib import admin
from django.urls import path
from blog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
]

Enter fullscreen mode Exit fullscreen mode

Django Admin

The Django admin interface is a powerful tool for managing your application's data without writing any additional code. It automatically generates a user-friendly interface for your models.

Setting Up Django Admin

  • Register Models: Register your models with the admin site to make them available in the admin interface.
# blog/admin.py

from django.contrib import admin
from .models import Post

admin.site.register(Post)

Enter fullscreen mode Exit fullscreen mode
  • Create a Superuser: Create a superuser to access the admin interface.
python manage.py createsuperuser

Enter fullscreen mode Exit fullscreen mode
  • Access the Admin Interface: Start the development server and navigate to http://127.0.0.1:8000/admin/. Log in with your superuser credentials to manage your data.

Conclusion

That is an overview of the process for writing an application in Django. Stay tuned for the next part of the series, where we will apply what we have learned to create a simple blog application.

Top comments (0)