|
| 1 | +- Install Django: If you haven't already installed Django, you can do so using pip, Python's package manager. You can install Django by running pip install django in your command line or terminal. |
| 2 | + |
| 3 | +- Create a Django Project: You can create a new Django project using the django-admin command-line utility. Navigate to the directory where you want to create your project and run the following command: |
| 4 | + |
| 5 | +```python |
| 6 | +django-admin startproject myproject |
| 7 | +``` |
| 8 | +- Create a Django App: Inside your Django project, you'll typically create one or more Django apps. Each app serves a specific functionality within your project. Navigate into your project directory and create a new app using the following command: |
| 9 | + |
| 10 | +```python |
| 11 | +python manage.py startapp myapp |
| 12 | +``` |
| 13 | +- Define a View: In Django, views are Python functions that take web requests and return web responses. Open the views.py file inside your app (myapp/views.py) and define a simple view like this: |
| 14 | + |
| 15 | +```python |
| 16 | + |
| 17 | +from django.http import HttpResponse |
| 18 | + |
| 19 | +def hello_world(request): |
| 20 | + return HttpResponse("Hello, World!") |
| 21 | +``` |
| 22 | +- URL Mapping: You need to map your view to a URL so that Django knows which view to execute when a specific URL is requested. Open the urls.py file inside your app (myapp/urls.py) and define a URL pattern like this: |
| 23 | + |
| 24 | +```python |
| 25 | +from django.urls import path |
| 26 | +from . import views |
| 27 | + |
| 28 | +urlpatterns = [ |
| 29 | + path('', views.hello_world, name='hello_world'), |
| 30 | +] |
| 31 | +``` |
| 32 | +- Include the App URLs in the Project URLs: Open the urls.py file inside your project (myproject/urls.py) and include the URLs of your app like this: |
| 33 | + |
| 34 | +```python |
| 35 | +Copy code |
| 36 | +from django.contrib import admin |
| 37 | +from django.urls import include, path |
| 38 | + |
| 39 | +urlpatterns = [ |
| 40 | + path('admin/', admin.site.urls), |
| 41 | + path('', include('myapp.urls')), |
| 42 | +] |
| 43 | +``` |
| 44 | +- Run the Development Server: Start the Django development server by running the following command in your project directory: |
| 45 | + |
| 46 | +```python |
| 47 | +python manage.py runserver |
| 48 | +``` |
| 49 | +Access the Hello World Page: Open your web browser and go to http://127.0.0.1:8000/ (or http://localhost:8000/). You should see "Hello, World!" displayed on the page. |
0 commit comments