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.
This blog will delve into the foundational aspects of Django, including its project structure, MVT architecture, and the Django admin interface.
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/
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 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 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 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 %}
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'), ]
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.
# blog/admin.py from django.contrib import admin from .models import Post admin.site.register(Post)
python manage.py createsuperuser
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.
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