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
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
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, 101 views
Next post: 3. Creating a new hello app
Previous post: 1. Setting up Django
Advanced Web Development index
- 38. Writing API tests
- 37. Testing in Django
- 36. Class-based views in the Django REST framework
- 35. Building a RESTful web service in Django
- 34. Introduction to CRUD, REST and APIs
- 33. Refactoring with generic views in Django
- 32. Django validators
- 31. Django forms (2) – using the ModelForm class
- 30. Django forms (1)
- 28. Adding CSS to the template
- 27. Django templating
- 26. Deleting and updating records
- 25. Joins, filters and chaining commands
- 24. Using the ORM in views.py
- 23. Adding to the database by writing a script
- 22. Adding to the database with Django Admin
- 21. Migrations
- 20. ORM – work through example
- 19. An introduction to the Object-Relational Mapper
- 18. Altering the database
- 17. SQL functions and summaries
- 16. SQL Query performance
- 15. Queries and table joins in SQL
- 14. Inserts and queries in SQL
- 13. Good practice in relational database design
- 12. Limitations to database modelling
- 11. Building a database using SQL
- 10. Introduction to PostgreSQL
- 9. How to start writing a new application in Django
- 8. Building a lightweight project
- 7. Django URLs
- 6. Django templates
- 5. Django models
- 4. Django views
- 3. Creating a new hello app
- 2. Creating a new virtual environment
- 1. Setting up Django