Skip to content

Commit f6da6d8

Browse files
committed
Extend documentation on URL mapping and views
Expanded the section on URL mapping and views to include a detailed walkthrough of creating an endpoint up to Django server running. Provided specific codes for 'music' app url patterns, setting up 'urls.py' in music folder, and implementing function view 'index'. Also introduced the upcoming section on Django Rest Framework usage.
1 parent 3213041 commit f6da6d8

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

docs/images/my_first_api.png

6.96 KB
Loading

docs/index.md

+42-3
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,48 @@ class Song(models.Model):
155155

156156
## URL Mapping and Views
157157

158-
- 🎬 Lecture : Explanation of URL configurations and Views/ViewSets in Django.
159-
- 💻 Exercise : Students implement URL mapping and views for a sample API.
160-
- 💡 Purpose: Practical understanding of Django's routing and controller mechanisms.
158+
Now let's go to the URL Mapping, we need to associate the url with the handler functions that are called as view in Django. To create a simple endpoint that works.
159+
160+
161+
```python
162+
# first_api.urls.py
163+
from django.contrib import admin
164+
from django.urls import path, include
165+
166+
urlpatterns = [
167+
path("admin/", admin.site.urls),
168+
path("", include('music.urls')),
169+
]
170+
```
171+
172+
You need to create a file `urls.py` inside music folder and make it look like the example below.
173+
174+
```python
175+
# music.urls.py
176+
from django.urls import path
177+
178+
from . import views
179+
180+
urlpatterns = [
181+
path('', views.index, name='index')
182+
]
183+
```
184+
185+
And last but not least you need to create this
186+
you need to create the view. A function view in this case.
187+
188+
```python
189+
from django.http import HttpResponse
190+
191+
192+
def index(_request):
193+
return HttpResponse("My first API!")
194+
```
195+
196+
So no you can use the command `task r` to start our django server, so you can access http://127.0.0.1:8000/ to see it.
197+
![my_first_api.png](images/my_first_api.png)
198+
199+
Until here we just looked at Django stuff. Now we will dive into Django Rest Framework(DRF) stuff.
161200

162201
## Serializers
163202

0 commit comments

Comments
 (0)