How to create a Django project and app (UPDATE)
Title: Create a Django project and app - A Beginner's Guide (UPDATED)Django is a free and open source web framework written in Python used for building web applications. It follows the Model-View-Controller (MVC) architectural pattern, also known as Model-View-Template (MVT) pattern. Django is known for its quick and hassle-free development process, scalability, and ease of use. In this guide, you will learn how to create a Django project and app in a step-by-step manner.Note: This tutorial is for Django version 3.2 or above. In this updated version, the command to create a Django project has been changed.Table of Contents:1. Prerequisites2. Setting up a virtual environment3. Installing Django4. Creating a Django project5. Creating a Django app6. Configuring the app7. Creating the models.py file8. Creating the views.py file9. Creating the urls.py file10. Creating the templates folder11. Conclusion1. PrerequisitesBefore we begin, make sure you have the following installed on your machine:- Python 3.6 or above (available from https://www.python.org/)- Pip package manager (available from https://pip.pypa.io/en/stable/installing/)- Virtualenv package (available from https://virtualenv.pypa.io/en/latest/installation.html)We recommend using a virtual environment for your Django project. This will ensure that all the packages used in your project are isolated from other projects on your machine.2. Setting up a virtual environmentOpen a terminal or command prompt and navigate to the directory where you want to create the virtual environment.Create a virtual environment using the following command:
python3 -m venv myenvHere, `myenv` is the name of the virtual environment. You can name it anything you want.Activate the virtual environment by running the following command:For Linux or Mac:source myenv/bin/activateFor Windows:myenv\Scripts\activateOnce you activate the virtual environment, you should see the name of the environment displayed in the terminal or command prompt.3. Installing DjangoIn the activated virtual environment, run the following command to install Django:pip install djangoThis will install the latest version of Django in your virtual environment.4. Creating a Django projectNow that you have Django installed, you can create a new Django project using the following command:django-admin startproject myprojectHere, `myproject` is the name of the project. You can name it anything you want.This will create a new directory named `myproject` in your current directory, with the following structure:myproject/ manage.py myproject/ __init__.py settings.py urls.py asgi.py wsgi.py `manage.py` is a command-line utility that lets you interact with your Django project. `myproject/settings.py` contains the settings for your project, such as database configuration and installed apps.5. Creating a Django appNow that you have a Django project created, you can create a Django app using the following command:python manage.py startapp myappHere, `myapp` is the name of the app. You can name it anything you want.This will create a new directory named `myapp` in your project directory, with the following structure:myapp/ __init__.py admin.py apps.py models.py tests.py views.py `myapp/models.py` will contain the models for your app, while `myapp/views.py` will contain the views for your app.6. Configuring the appOpen `myproject/settings.py` file in your text editor and add `myapp` to the `INSTALLED_APPS` setting:INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'myapp', ]This will register your app with Django and makes it available to your project.7. Creating the models.py fileOpen `myapp/models.py` file in your text editor and define the models for your app. Here's a simple example:from django.db import models class MyModel(models.Model): name = models.CharField(max_length=100) description = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) def __str__(self): return self.nameThis defines a model named `MyModel` with three fields - `name`, `description`, `created_at`, and `updated_at`.8. Creating the views.py fileOpen `myapp/views.py` file in your text editor and define the views for your app. Here's a simple example:from django.shortcuts import render from .models import MyModel def home(request): myobjects = MyModel.objects.all() return render(request, 'home.html', {'myobjects': myobjects}) This defines a view named `home` that retrieves all `MyModel` objects and passes them to a template named `home.html`.9. Creating the urls.py fileOpen `myapp/urls.py` file in your text editor and define the URLs for your app. Here's a simple example:from django.urls import path from . import views urlpatterns = [ path('', views.home, name='home'), ]This defines a URL pattern that maps the root URL to the `home` view.10. Creating the templates folderCreate a new directory named `templates` in your app directory. Inside the `templates` directory, create a new file named `home.html` and add the following content:<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>MyApp Home</title> </head> <body> <h1>MyApp Home</h1> <ul> {% for myobject in myobjects %} <li>{{ myobject.name }}</li> {% endfor %} </ul> </body> </html>This defines a simple HTML template that displays the name of all `MyModel` objects.11. ConclusionIn this tutorial, you learned how to create a Django project and app, configure the app with models, views, and URLs, and create a simple HTML template for rendering the data. Django provides a lot of built-in functionality that makes it easy to develop web applications quickly and efficiently. You can build on this foundation to create more complex web applications that meet your specific needs. Happy coding!