Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 22, 2022 01:31 pm GMT

How to start a Django project

Hello!
In this post I'll show you how to start a project using python and Django.

For this tutorial, you have to have python already installed in your computer.

Step 1: Create a directory for your project and enter it:

mkdir project_namecd project_name

Step 2: Create a virtual environment in your project folder:
Unlike node.js, where the node_modules folder is created automatically when you run the command to start a project (yarn init or npm init), when using python and django, to have your project dependencies installed locally in your project directory instead of globally in your computer, you have to create your own virtual environment from scratch.

python -m venv venv

Step 3: Activate your virtual environment:

source venv/bin/activate

Step 4: Install the basic dependencies:

pip install djangorestframework black ipython

I like using djangorestframework because it makes rest API development so much easier, and already installs django automatically.

Step 5: Create a file named requirements.txt, so that pip knows which dependencies to install if you run your project in another machine:

pip freeze > requirements.txt

Step 6: Start your project;

django-admin start project name_of_your_project .

Since we already prepared the environment in our directory, I like to use the dot in the end of the start project command, to tell django to start the app in my current folder. If I don't use it, django makes another directory inside my current directory, which in our case is unnecessary.

Extra tip: Don't forget to create a gitignore, and put the venv directory in it, as well as the pycache, and your db.sqlite3 (once your run your migrations, django automatically generates a file named db.sqlite3, which is NOT recommended to upload to git)

That's it for today, I hope I helped you in some small way!


Original Link: https://dev.to/nehamamandelbaum/how-to-start-a-django-project-26ha

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To