Understanding Django Fundamentals

王林
Release: 2024-07-26 11:15:21
Original
323 people have browsed it

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/

Copy after login
  • 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

Copy after login

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

Copy after login

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>

Copy after login

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

Copy after login

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)

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

Copy after login
  • 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.

The above is the detailed content of Understanding Django Fundamentals. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!