![](uploads/django-6651b00a28de4.png)
Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It follows the DRY (Don't Repeat Yourself) principle and focuses on automating as much as possible to help developers build web applications quickly and efficiently.
To start a new Django project, you can use the following command:
django-admin startproject projectname
This will create a new Django project with the given project name. You can then navigate into the project directory and start the development server using the following command:
python manage.py runserver
This will start the development server at http://127.0.0.1:8000/
. You can access your Django application by visiting this URL in a web browser.
In Django, an app is a web application that does something - e.g., a blog, a poll, a forum, etc. To create a new app in Django, you can use the following command:
python manage.py startapp appname
This will create a new Django app with the given app name. You can then define models, views, and templates for your app to implement its functionality.
In Django, models are Python classes that represent database tables. You can define models in your Django app by creating a models.py
file and defining your model classes inside it. Here's an example of a simple model in Django:
```python from django.db import models class Product(models.Model): name = models.CharField(max_length=100) price = models.DecimalField(max_digits=10, decimal_places=2) description = models.TextField() ```
After defining your models, you can create database tables by running the following command:
python manage.py makemigrations
python manage.py migrate
In Django, views are Python functions that take a web request and return a web response. You can define views in your Django app by creating a views.py
file and defining your view functions inside it. Here's an example of a simple view in Django:
```python from django.http import HttpResponse def hello(request): return HttpResponse("Hello, World!") ```
You can then map your views to URLs by defining URL patterns in your Django project's urls.py
file.
In Django, templates are HTML files that are used to generate the final web page that is sent to the user's browser. You can create templates in your Django app by creating a templates
directory and placing your HTML template files inside it. Here's an example of a simple template in Django:
```html
```
You can then render your templates in views using Django's template engine to generate dynamic web pages.
Django provides a built-in admin