Difference between revisions of "Creating a Django Web App"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Create an app)
(Create an app)
Line 95: Line 95:
  
 
Now open a browser and enter the ip address above, ie http://127.0.0.1:8000 :
 
Now open a browser and enter the ip address above, ie http://127.0.0.1:8000 :
 +
 
[[File:Capture 8.gif]]
 
[[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]]

Revision as of 16:30, 12 April 2019

Setup - Visual Studio

Create a new project in Visual Studio, look in the python section and Django:

Django1.gif

On your own machines, this will install all of the appropriate packages into your python installation if you select 'Install into Python'. However in College will will need to do a bit more, and instead choose 'install into virtual environment':

Vswebapp1.png

In College we will need see this window:

Vswebapp2.png

You can untick the install packages option, because this will fail on college computers.

Now, find the site packages folder within your project folder:

Django4.gif

Extract this zip file: Django 1.11.20

You should now have:

Django5.gif

Next, you will need to create a super user account. This must be done before running the app for the first time:

Django6.gif

Now click start in Visual Studio, you should see something like this:

Django2.gif

It should automatically open a browser, it should already point to your web app:

Django3.gif

Setup - Manually on Own Machine

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':

Capture1.gif

Now click 'Environment Variables':

Capture2.gif

Now select Path and click edit:

Capture 3.gif

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:

Capture 4.gif

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:

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:

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:

Capture 7.gif

Now open a browser and enter the ip address above, ie http://127.0.0.1:8000 :

Capture 8.gif

Change the url to 'http://127.0.0.1:8000/admin' to access the admin features:

Capture 10.gif

You should be able to login with your superuser details:

Capture 11.gif