Starting Your First Project - potatoscript/django GitHub Wiki
🧑💻 Step 1: Create Your Django Project Folder
Before we get started, we need a special space for your project. This is like creating a new folder to keep your school assignments organized! 🎒
- Open your Command Prompt (Windows) or Terminal (Mac/Linux).
- Let’s create a folder for our new Django project. Type:
mkdir mysite
cd mysite
This makes a folder called mysite
and enters it.
Now you’re ready to start building your project!
🧩 Step 2: Create a New Django Project
Now that you have your folder, let’s use Django to set up your project.
- Run the following command:
django-admin startproject mysite .
What did we just do?
startproject
: Tells Django to start a new project.mysite
: The name of your project (You can change this to anything you like)..
: Means the project files will be created right in the folder you’re in (no new folder created).
Now, inside your mysite/
folder, you’ll see:
File | What It Does |
---|---|
manage.py |
The magic file that controls your project (like a remote control). |
mysite/ |
This folder contains the actual project code. |
settings.py |
This is where you set up rules for your project, like language, timezone, etc. |
urls.py |
Defines the URLs (links) that users can visit on your website. |
wsgi.py |
For running the project on a server (we won’t need this yet!). |
🔥 Step 3: Run the Django Development Server
Time to see your project live! 🖥️
- In your Terminal, inside the
mysite/
folder, run:
python manage.py runserver
- You should see something like this:
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
- Open your web browser, and type:
http://127.0.0.1:8000/
You should see the magical Django “It worked!” page.
This means your project is alive and kicking! 🎉
📝 Step 4: Understand the Project Files
Let’s break down the important files in your project:
-
mysite/settings.py
This is like the control center for your project. You’ll tell Django what your website should look like, what database to use, and more. -
mysite/urls.py
This file helps Django know what to do when someone types a URL. It’s like a map for your website! You’ll connect views (pages) to specific URLs here. -
manage.py
This is your remote control. Every time you want to run something in Django (like starting the server or making changes), you’ll usemanage.py
to tell Django what to do.
🌱 Step 5: Create Your First App
Now that your project is up and running, let’s build an App inside it! 🎨
What’s an App?
An App is like a small piece of your website that does one specific job. It can be something like a Blog, Login page, or Store.
For now, let’s build a simple one called hello.
- In the Terminal, run:
python manage.py startapp hello
This will create a new folder called hello
in your project with files like:
File | What It Does |
---|---|
views.py |
This is where you write the code that shows what to display on the page. |
urls.py (We’ll create this) |
Where you connect URLs to views (the map we talked about). |
🧑💻 Step 6: Create Your First View
Let’s create a view for your hello app. Views are like the pages on your website.
- Open
hello/views.py
and add this code:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, welcome to my Django website! 🎉")
This view says: "When someone goes to this page, show them ‘Hello, welcome to my Django website! 🎉’".
📍 Step 7: Link the View to a URL
Now let’s make this view show up when someone visits the website.
- In the
hello/
folder, create a new file calledurls.py
. Insidehello/urls.py
, add this code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
]
This code says: "When someone goes to the homepage (/
), show the home view."
🧭 Step 8: Link Your App to the Main URL File
Now we need to tell the main project about the new hello app and its URLs.
- Open
mysite/urls.py
, and change it to look like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello.urls')), # Link the hello app here!
]
This tells Django: "When someone goes to the homepage, go to hello.urls
."
🎉 Step 9: See It in Action
- Make sure the server is still running:
python manage.py runserver
- Open your web browser again and go to:
http://127.0.0.1:8000/
🎉 You should now see: "Hello, welcome to my Django website! 🎉"