Mada za sehemu hiiIdentifying and setting up a web development environmentMada 4
A basic project is a minimal setup for a web application that includes essential files and folder structure required to start building and testing features. Creating a basic project helps you understand how a framework organizes files and handles code, and it lets you run and test your code on a development server.
A basic project provides the foundation for building a web application. It contains:
- A project folder that holds all files
- Configuration files that tell the framework how to work
- A main script that starts the application
- A template or view that displays content in the browser
Different frameworks organize these items differently. In this section, we will create a basic project using the Django framework to display a simple "Hello" message.
The following steps show how to create a simple "Hello" web application using Django. Django is a Python web framework that is beginner-friendly and follows the Model-View-Template (MVT) structure.
Step 1: Create a New Django Project
Open your command prompt and type the following commands:
django-admin startproject myFirstDjango
cd myFirstDjango
This creates a new Django project folder called myFirstDjango and moves you into that folder.
Step 2: Create a New App
Inside the project, create a new app by typing:
python manage.py startapp hello
This creates a new Django app called hello within the project. An app is a component that performs a specific function within the project.
Step 3: Create a Simple View
A view is a function that receives a web request and returns a response. Open the file hello/views.py in VS Code and type the following code:
from django.shortcuts import render
from django.http import HttpResponse
def print_message(request):
return HttpResponse("Hello students! Are you fine?")
This view function called print_message returns a simple HTTP response containing the message "Hello students! Are you fine?".
Step 4: Create URL Configuration for the App
Django needs to know which view to display when a user visits a particular URL. Create a new file called urls.py inside the hello folder and add this code:
from django.urls import path
from . import views
urlpatterns = [
path('', views.print_message),
]
This code maps the root URL of the app (/hello/) to the print_message view.
Step 5: Register the App in Settings
Django must know about your app before it can use its views and URLs. Open myFirstDjango/settings.py and find the INSTALLED_APPS list. Add 'hello' to that list:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
...
'hello',
]
Step 6: Link the App to the Project URL
Open the project's main URL configuration file located at myFirstDjango/myFirstDjango/urls.py. Add code to include the hello app's URLs:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', include('hello.urls')),
]
This tells Django to check hello/urls.py when someone visits /hello/.
Step 7: Run the Development Server
Start your Django development server by typing:
python manage.py runserver
Step 8: View Your Project
Open your web browser and visit:
http://127.0.0.1:8000/hello/
You should see the message "Hello students! Are you fine?" displayed in your browser. This confirms that your basic project is working correctly.
When setting up projects, you may encounter some common errors:
- Command not found: Make sure Python and Django are installed and added to your system's PATH.
- Port already in use: If port 8000 is busy, run the server on a different port:
python manage.py runserver 8001 - Missing dependencies: Install required packages using
pip install <package-name> - Permission denied: Right-click Command Prompt and select "Run as Administrator" on Windows.
Now try building your own basic project following these steps:
- Set up a new web project using any framework (Django, Laravel, or React).
- Create a simple view or route that returns a message like "I am learning web frameworks!"
- Run your project and verify it displays the message correctly in the browser.
- Fix any errors you encounter during testing.
In groups, work together to enhance your basic projects by adding:
- A form to collect user input (such as a name or feedback)
- Basic CSS styling to improve the appearance of your page
This practice helps you understand how frameworks organize code and prepares you to build more advanced web applications.
In Tanzania, web development skills are increasingly important for small businesses. For example, a local shop owner in Arusha could use a basic Django project to create a simple online catalog for their products, allowing customers to browse items and place orders through a web interface instead of manually writing orders in a notebook.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Create and run a basic project within the framework.
- Apply basic framework features (e.g., routing, rendering) to the created project.
Tathmini ya matokeo
A simple working project demonstrating the use of the framework’s features (e.g., rendering a page, handling routes).
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain
- steps to start a project, including setting up directories, configurations, and initial code.
- Verify functionality of the installed framework by running a test project
Principles: The student should explain the principles of:
- simplicity and functionality in the initial project.
Theories: The student should explain:
- basic framework features (e.g., routing, rendering)
Circumstantial knowledge:
Detailed knowledge about:
- debugging, testing, and iteration phases of the SDLC
Swali
What is the first command you type in the command prompt to create a new Django project?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado