"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Understanding Django Fundamentals

Understanding Django Fundamentals

Published on 2024-07-31
Browse:734

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/

  • 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

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})

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 Home

Blog Posts

{% for post in posts %}

{{ post.title }}

{{ post.content }}

Published on: {{ post.published_date }}

{% endfor %}

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'),
]

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)

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

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

Release Statement This article is reproduced at: https://dev.to/kihuni/understanding-django-fundamentals-27h0?1 If there is any infringement, please contact [email protected] to delete it
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3