DEV Community

Krishna Bhamare
Krishna Bhamare

Posted on

Django Quick Start Guide๐Ÿ˜Ž

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • Once Django is installed, you can create a new Django project by running the following command:
django-admin startproject myproject
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Once you have created your app, you will need to add it to the INSTALLED_APPS list in your Django project's settings.py file. This will tell Django to include the app in the project.
INSTALLED_APPS = [
    ...
    'myapp',
]
Enter fullscreen mode Exit fullscreen mode
  • Finally, you will need to run the migrate command to create the necessary database tables for your app.
$ python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

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!!!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where youโ€™ll build it, break it, debug it, and fix it. Youโ€™ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good olโ€™ AI to find and fix issues fast.

RSVP here โ†’

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up