Krishna Bhamare
Posted on January 9, 2023
Before you can create a new Django project, you will need to have the following installed on your system:
- Python: Django is a Python web framework, so you will need to have Python installed on your system. You can check if you have Python installed by running the following command in your terminal:
python --version
If Python is not installed, you can download it from the official Python website at https://www.python.org/.
Create Virtual Environment:
To create a virtual environment for a Django project, follow these steps:
- First, make sure you have Python and pip installed on your system. You can check if you have them by running the following commands:
pip --version
- Once you have Python and pip installed, you can create a virtual environment by running the following command:
python -m venv myenv
or
py -m venv myenv // Windows OS
This will create a new directory called "myenv" that contains a Python virtual environment. Replace "myenv" with the name you want to give to your virtual environment.
To activate the virtual environment, run the following command:
source myenv/Scripts/activate
Replace "myenv" with the name of your virtual environment.
Create Django Project:
- Now that your virtual environment is activated, you can install Django using pip:
pip install django
- Once Django is installed, you can create a new Django project by running the following command:
django-admin startproject myproject
- This will create a new directory called "myproject" that contains the skeleton of a Django project. Replace "myproject" with the name you want to give to your project.
At the root of the project, you'll typically find the following files and directories:
-
manage.py
: A command-line utility that allows you to interact with the project, such as running the development server, creating database tables, and running tests. -
settings.py
: Contains the project's configuration, including settings for the database, installed apps, middleware, and other options. -
urls.py
: Defines the URL patterns for the project. These patterns map URLs to views, which handle the request and return a response. -
wsgi.py
: A file that defines the WSGI application for the project. This file is used to deploy the project to a production server. In addition to these files, you'll also typically find a
.gitignore
file, which tells Git which files and directories to ignore when committing changes to the project.To verify that everything is working, you can start the development server by running the following command:
python manage.py runserver
This will start the development server at http://127.0.0.1:8000/. You should see a page with the message "Welcome to Django".
Deactivate Virtual Env:
- To deactivate a virtual environment in Django, you can use the deactivate command. This command is provided by the venv module, which is included in the Python standard library.
- Here's an example of how to use deactivate to exit a virtual environment:
$ source myenv/Scripts/activate
(myenv) $ # your virtual environment is now active
(myenv) $ # do some work here
(myenv) $ deactivate
$ # your virtual environment is now deactivated
- Alternatively, you can use the exit command to exit the virtual environment. This will deactivate the environment and close the terminal window or shell.
(myenv) $ exit
Keep in mind that the virtual environment must be activated before you can use the deactivate or exit command.
Create an DJango app:
- To create an app in a Django project, you can use the
django-admin startapp
command. This command creates a new directory with the given app name and generates the basic files needed for a Django app. - Here's an example of how to use
django-admin startapp
to create an app called "myapp":
$ django-admin startapp myapp
- This will create a new directory called "myapp" with the following files:
myapp/
βββ __init__.py
βββ admin.py
βββ apps.py
βββ migrations/
β βββ __init__.py
βββ models.py
βββ tests.py
βββ views.py
- Once you have created your app, you will need to add it to the
INSTALLED_APPS
list in your Django project'ssettings.py
file. This will tell Django to include the app in the project.
INSTALLED_APPS = [
...
'myapp',
]
- Finally, you will need to run the
migrate
command to create the necessary database tables for your app.
$ python manage.py migrate
This will create the database tables needed for your app, as well as any additional tables needed by Django itself.
I hope this helps!
Thanks!!!
Posted on January 9, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
February 22, 2023