Tech With Tim Logo
Go back

Setup, Installation & Page Navigation

What is Django

Django is a very popular and widely used full stack web development framework for python. It is used by large companies like Instagram and pintrest! The main advantage of django is that you program almost everything using only python.

Installing Django

Django is a python module and can be installed via pip. Simply open your command prompt or terminal and type "pip install django". kuva_2023-04-25_143627793.png If you are having issues with this watch the following video to help you get pip working. How to Fix Pip

Setting up a Project

The first thing we need to do when we want to start using django is setup a project. To do this we will need to create a directory somewhere to store our django project. Once we've done that we will need to open our command prompt and change into that directory. Now we will type the following into cmd or terminal: "django-admin startproject " kuva_2023-04-25_144159349.png

Next we will change directories again into the name of our project. Once we've done this we can test that our installation was successful by running the command "python manage.py runserver" and going to the highlighted url in our web browser. kuva_2023-04-25_144228771.png If you are successfully able to connect to the url then you are all done setting up a django project!

Creating an App

Now that we have created a django project we need to create a django app. An app is what will have pages and views and represent your site. Whereas the project is kind of an environment that runs your app! Note that you can multiple apps within one django project.

To create an app make sure you are in the same directory as the manage.py file and type the following: "python manage.pt startapp " kuva_2023-04-25_144321590.png

Creating a View

Now that we've created an app we can start modifying some files within our app to create our first web page (aka view).

To do this we will go into our apps root directory and modify the file called views.py.

# views.py file
from django.http import HttpResponse


def index(request):
    return HttpResponse("Tech with tim!")

Linking to our View

Now that we've created a view we need to create a url to link to it. To do this we need to create a new python file called urls.py. This file will be in root directory of our app (same place as views.py). It should contain the code shown below.

# urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

The last step is to exit this directory and go into the interior directory of our site name. Here we should see a file called urls.py (a different one that the one we just created). We need to modify it to be the following:

# urls.py 
from django.contrib import admin
from django.urls import include, path

urlpatterns = [
    path('', include('main.urls')), # main will be the name of your app
    path('admin/', admin.site.urls),
]

Finally we can re run our server and see a web page that says Tech with tim! kuva_2023-04-25_144455752.png

Design & Development by Ibezio Logo