Heim >Backend-Entwicklung >Python-Tutorial >Django-Grundlagen verstehen
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/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>
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.
Das obige ist der detaillierte Inhalt vonDjango-Grundlagen verstehen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!