Difference between revisions of "Creating a Django Web App"
(→Install Django) |
(→Setup - Visual Studio with new template) |
||
(23 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | =Setup - Visual Studio= | + | =Setup - Manually Within Visual Studio= |
− | |||
− | + | follow the following to create a new python project and to create a virtual environment: | |
− | + | [[Create a python project in Visual Studio]] | |
− | [[ | + | [[Creating a virtual environment in Visual Studio]] |
− | '' | + | The last link above also shows how to create a 'setup.py' to install Django using pip, use this method to install Django. |
− | [ | + | ===In College=== |
+ | You will need to complete the above links, but the installation will not work. You will need to install django by replacing the contents of your environment folder with | ||
+ | [https://studentthomrothac-my.sharepoint.com/:u:/g/personal/wayne_jones_thomroth_ac_uk/EVoEVPiGN2ZJnA415Yduf_8BFQyWcnXlyDMD37mwjQ2STg?e=u7VuQm This]. College are currently blocking the ports required to install modules into your python environment. | ||
− | + | '''Easy Method:''' just use [https://studentthomrothac-my.sharepoint.com/:u:/g/personal/wayne_jones_thomroth_ac_uk/ESxjsT-IKulAhg1Ryk6-fUcBH3QCP95hEGh9kBm247EPUQ?e=znjdSB this] project for yours. | |
− | Now | + | ==Management== |
+ | Now use this program: [[Django management program]] to create a management app to run the key commands needed for django. | ||
− | + | ===New Project=== | |
+ | * Run the management program and choose '1' as your first option to run django_admin | ||
+ | * Now selected option 1 to 'StartProject' | ||
+ | * Now enter a project name, eg 'MyProject' | ||
− | + | ===New App=== | |
+ | * Run the management program and choose '1' as your first option to run django_admin | ||
+ | * Now selected option 1 to 'StartApp' | ||
+ | * Now enter a app name, eg 'MyApp' | ||
− | + | ===Run Server=== | |
+ | * Run the management program and choose '2' as your first option to run manage | ||
+ | * Now selected option 1 to 'RunServer' | ||
− | + | ===Make Migrations=== | |
+ | * Run the management program and choose '2' as your first option to run manage | ||
+ | * Now selected option 2 to 'MakeMigrations' | ||
− | + | ===Migrate=== | |
+ | * Run the management program and choose '2' as your first option to run manage | ||
+ | * Now selected option 3 to 'Migrate' | ||
− | + | ===Create Super User=== | |
+ | * Run the management program and choose '2' as your first option to run manage | ||
+ | * Now selected option 4 to 'CreateSuperUser' | ||
− | + | ==You Will Need to== | |
+ | You will need to run option 1 & StartProject followed by StartApp, Then option 2 & Migrate followed by RunServer. | ||
− | + | Finally so that you can see all of the files within the project, make sure 'All Files' is selected in solution explorer: | |
− | + | [[File:Show all files.png]] | |
− | + | You can also set your django management program as the startup file, by right clicking it in solution explorer and then click 'Set as Startup File'. | |
=Setup - Manually on Own Machine= | =Setup - Manually on Own Machine= | ||
+ | This will create a web app based on Django 2.2, this is the latest version of Django and isn't entirely compatible with the Django 1.11 used by Visual Studio. | ||
+ | |||
==Setup the Path== | ==Setup the Path== | ||
You will need to ensure your system path is set to access the python folder and the scripts folder. Search for 'path' in the search bar, and click 'edit the system variables': | You will need to ensure your system path is set to access the python folder and the scripts folder. Search for 'path' in the search bar, and click 'edit the system variables': | ||
Line 63: | Line 82: | ||
==Create an app== | ==Create an app== | ||
+ | |||
+ | With Django installed above, it should add django-admin into your scripts folder. We will use this to create a new Django app. But first we need to create a folder for your app, and then run the command prompt and go to the folder location: | ||
+ | |||
+ | [[File:Capture 5.gif]] | ||
+ | |||
+ | Now we can run this command to create a blank Django app: | ||
+ | |||
+ | django-admin startproject myapp | ||
+ | |||
+ | This will create a folder called myapp, and it will contain everything required for a Django app. If you go into the myapp directory you will see manage.py: | ||
+ | |||
+ | [[File:Capture 6.gif]] | ||
+ | |||
+ | Now we need to run a few commands, firstly: | ||
+ | |||
+ | manage.py migrate | ||
+ | |||
+ | Then we need to create a superuser, so now type this command: | ||
+ | |||
+ | manage.py createsuperuser | ||
+ | |||
+ | You will need to follow the instructions to create a username and password. | ||
+ | |||
+ | Now to run the app you will need to run the command: | ||
+ | |||
+ | manage.py runserver | ||
+ | |||
+ | This will start the server and tell you the address to access the app: | ||
+ | |||
+ | [[File:Capture 7.gif]] | ||
+ | |||
+ | Now open a browser and enter the ip address above, ie http://127.0.0.1:8000 : | ||
+ | |||
+ | [[File:Capture 8.gif]] | ||
+ | |||
+ | Change the url to 'http://127.0.0.1:8000/admin' to access the admin features: | ||
+ | |||
+ | [[File:Capture 10.gif]] | ||
+ | |||
+ | You should be able to login with your superuser details: | ||
+ | |||
+ | [[File:Capture 11.gif]] |
Latest revision as of 11:45, 18 November 2020
Contents
Setup - Manually Within Visual Studio
follow the following to create a new python project and to create a virtual environment:
Create a python project in Visual Studio
Creating a virtual environment in Visual Studio
The last link above also shows how to create a 'setup.py' to install Django using pip, use this method to install Django.
In College
You will need to complete the above links, but the installation will not work. You will need to install django by replacing the contents of your environment folder with This. College are currently blocking the ports required to install modules into your python environment.
Easy Method: just use this project for yours.
Management
Now use this program: Django management program to create a management app to run the key commands needed for django.
New Project
- Run the management program and choose '1' as your first option to run django_admin
- Now selected option 1 to 'StartProject'
- Now enter a project name, eg 'MyProject'
New App
- Run the management program and choose '1' as your first option to run django_admin
- Now selected option 1 to 'StartApp'
- Now enter a app name, eg 'MyApp'
Run Server
- Run the management program and choose '2' as your first option to run manage
- Now selected option 1 to 'RunServer'
Make Migrations
- Run the management program and choose '2' as your first option to run manage
- Now selected option 2 to 'MakeMigrations'
Migrate
- Run the management program and choose '2' as your first option to run manage
- Now selected option 3 to 'Migrate'
Create Super User
- Run the management program and choose '2' as your first option to run manage
- Now selected option 4 to 'CreateSuperUser'
You Will Need to
You will need to run option 1 & StartProject followed by StartApp, Then option 2 & Migrate followed by RunServer.
Finally so that you can see all of the files within the project, make sure 'All Files' is selected in solution explorer:
You can also set your django management program as the startup file, by right clicking it in solution explorer and then click 'Set as Startup File'.
Setup - Manually on Own Machine
This will create a web app based on Django 2.2, this is the latest version of Django and isn't entirely compatible with the Django 1.11 used by Visual Studio.
Setup the Path
You will need to ensure your system path is set to access the python folder and the scripts folder. Search for 'path' in the search bar, and click 'edit the system variables':
Now click 'Environment Variables':
Now select Path and click edit:
Now look on your system to find where Python is installed, make a note of the folder and also check to see if it also has a script folder. Now add these values to the Path:
Install Django
Now to install Django you can enter the following command:
python -m pip install django
If you have set the path above it should work, you might need to restart your computer after you change the path. This command should install Django and all of its dependencies.
Create an app
With Django installed above, it should add django-admin into your scripts folder. We will use this to create a new Django app. But first we need to create a folder for your app, and then run the command prompt and go to the folder location:
Now we can run this command to create a blank Django app:
django-admin startproject myapp
This will create a folder called myapp, and it will contain everything required for a Django app. If you go into the myapp directory you will see manage.py:
Now we need to run a few commands, firstly:
manage.py migrate
Then we need to create a superuser, so now type this command:
manage.py createsuperuser
You will need to follow the instructions to create a username and password.
Now to run the app you will need to run the command:
manage.py runserver
This will start the server and tell you the address to access the app:
Now open a browser and enter the ip address above, ie http://127.0.0.1:8000 :
Change the url to 'http://127.0.0.1:8000/admin' to access the admin features:
You should be able to login with your superuser details: