the red penguin
HOME ABOUT SITEMAP BLOG LOGIN

2. Creating a new virtual environment

I used some of this video: https://www.youtube.com/watch?v=LQTMqGns7Co

1. Create a new directory for the project

eg C:\Users\paulg.DESKTOP-AU8JD0H\Documents\django>

2. Go to CMD and navigate to the directory.

3. Create a virtual environment called env here

$ python -m venv env

4. To start this virtual environment type

$ env\Scripts\activate

We now get (env) before the prompt on the left.

5. We now need to install Django in the virtual environment.

$ pip install django

6. Now we can start the actual Django project. To do this we can start the project and give it a name. Type

$ django-admin startproject my_django_app

7. We can then open VSC and File/Open Folder to open this folder.

We can see a few .py files in the my_django_app directory and also manage.py outside this directory too.

8. We can run Django commands by going back to CMD, navigating to this folder and typing in a command to start the app.

$ cd my_django_app
$ python manage.py startapp hello

9. This adds a new directory. I need to go to settings for my_django_app and add this installed app. (We will do this in the next page).

10. To see Django working I need to go back to CMD and do this:

$ python manage.py runserver

There will be various messages but it will also say the server is running at http://127.0.0.1:8000/ . I can paste this into a browser tab:

I get a message saying “The install worked successfully! Congratulations!”

11. Creating a superuser

Next we stopped the server (CTRL+C) and did

$ python manage.py migrate
$ python manage.py createsuperuser

It then asked me to create a user name (paul), email and password.

12. I can now go to http://127.0.0.1:8000/admin and enter my username and password.

We can now see user admin.

Wednesday 13 October 2021, 484 views


Leave a Reply

Your email address will not be published. Required fields are marked *