[go: up one dir, main page]

0% found this document useful (0 votes)
38 views21 pages

Django Project and Subproject-Topic 1 Lab

The document provides instructions on setting up a Django web framework project including installing Python and Django, creating the project structure, running a development server, and creating templates and views for pages.

Uploaded by

Raziq Ridzuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views21 pages

Django Project and Subproject-Topic 1 Lab

The document provides instructions on setting up a Django web framework project including installing Python and Django, creating the project structure, running a development server, and creating templates and views for pages.

Uploaded by

Raziq Ridzuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

WAD

FRAMEWORK AND
INSTALLATION
Installation

1. Go to https://www.python.org/
➢ Install Python3
2. Create new folder - example project_wad
3. Get into the project using command prompt

4. Install Django using commad prompt


➢ pip3 install django / pip install django
How to create django project
1. Create a new project, example Personal_Homepage
➢ django-admin startproject Personal_Homepage
➢ J

➢ f
How to create django project

2. Go inside Personal_Homepage folder


➢ Using comman prompt – change directory
➢ cd Personal_Homepage
3. Run the server
➢ python manage.py runserver
4. Open browser

➢ Open this url on browser 127.0.0.1:8000


How to create django project
Open Folder
1. Open Visual Studio Code
2. File -> Open Folder
3. Look for the Personal_Hompage subfolder inside project_wad folder
4. Type select folder
5. Ok
Important files

a. manage.py
➢ help us to execute commands on our terminal. Don’t EDIT
this file!
b. settings.py
➢ contains some important configuration settings for our
new project, for example the database that we use. We will
use default settings first but at the end of the course we
might change a little bit (for deployment subject)
c. urls.py
➢ contains directions for where users should be routed after
navigating to a certain URL. We will change this a lot
Important files
b. settings.py
➢ contains some important
configuration settings for
our new project, for exam
ple the database that we
use. We will use default
settings first but at the
end of the course we
might change a little bit
(for deployment subject)
Important files
c. urls.py
➢ contains directions for where users should be routed after
navigating to a certain URL. We will change this a lot
Create subproject
• In a Django project (Personal_Hompage) you can have multiple subprojects,
eg: traveloka might have a flight subprojects, hotels subprojects etc..

• But normally in small to medium project, we only have 1 subproject

• We have to create the subproject


➢ python manage.py startapp hello_subproject
Important files

Important files will be generated


1. models.py
➢ To define our model (data/tables in
database)
2. views.py
➢ Controller (logic of our application)
Setup that you need to do
1. Add homepage inside Settings.py under “INSTALLED_APPS”
Setup that you need to do
2. Create and link url files

a) Add new file in (subproject) hello_subproject->


urls.py
b) Add the following code:

from django.urls import path


from . import views

urlpatterns = [
path("", views.index, name="index"),
]
Add codings inside urls.py in the subproject/app folder

from django.urls import path


from . import views

urlpatterns = [
path("", views.index, name="index"),
]
Setup that you need to do
Add codings inside urls.py in the personalhomepage

3. Go to urls in personal_hompage (project) and modify as follows:

from django.contrib import admin


from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path(‘my_page/',include('homepage.urls'))
]
Setup that you need to do
3. Create folder templates in subproject (hello_subproject)
4. In the templates folder, create a html file which is index.html
5. Create design for your main page
Setup that you need to do
6. Inside views.py in hello_subproject add the following code

from django.shortcuts import render

# Create your views here.

def index(request):
return render (request,"index.html")
Setup that you need to do
7. Save document (setting, urls, view, html )
8. Runserver using command prompt
➢ python manage.py runserver
Setup that you need to do
Try to add page about.

1. Create file about.html


2. Create design for your page
3. Edit your view.py

def about(request):
return render (request,“about.html")

4. Add path in homepage urls.py (subproject)

path(“about/", views.about, name=“about")

5. Edit index.html link for li About Me

<li><a href="{% url ‘about' %}">About Me</a></li>


6. Add link in page about to view page index.
Try this

Create page hobbies and link from homepage

You might also like