# project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('api.urls')),
path('blog/', include('blog.urls')),
]
# api/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('posts/', views.post_list, name='api_post_list'),
path('posts/<int:id>/', views.post_detail, name='api_post_detail'),
]URL Configuration and URL Routing: Understanding include() and path() patterns
The main urls.py uses include() to delegate URL routing to app-specific urls.py files. A request to '/api/posts/5/' matches the 'api/' prefix, then routes to api/urls.py where '<int:id>/' pattern matches
include() is used only for admin URLs and should not be used for app URLs
The '<int:id>/' pattern will match any string, not just integers
Each app must define all its URLs in the main project urls.py file instead of using include()
Show answer & explanationAnswer
Correct answer
The main urls.py uses include() to delegate URL routing to app-specific urls.py files. A request to '/api/posts/5/' matches the 'api/' prefix, then routes to api/urls.py where '<int:id>/' pattern matches
Explanation
The project's main urls.py uses include() to delegate routing to app-specific urls.py files. This modular approach keeps URLs organized. A request to '/api/posts/5/' matches 'api/' prefix, then includes api/urls.py where '<int:id>/' matches and calls post_detail view.
All 30 Django Project Structure and Setup questions
- 1.What is the primary purpose of the manage.py file in a Django project?
- 2.Which file in a Django project is used to store project-level settings and configurations?
- 3.What does the __init__.py file do in a Django app directory?
- 4.Which command is used to create a new Django app within a project?
- 5.What is the purpose of the models.py file in a Django app?
- 6.Where should static files like CSS and JavaScript be placed in a Django project?
- 7.What is the role of the urls.py file in a Django project?
- 8.Which file is responsible for WSGI application setup in a Django project?
- 9.What command should you run after creating a new Django app to register it in the project?
- 10.Where are HTML templates typically stored in a Django app?
- 11.Scenario:
- 12.What is the correct sequence of steps to initialize a Django project?
- 13.Hari is configuring a Django project with multiple databases.
- 14.Code-based:
- 15.What is the purpose of the apps.py file in a Django app?
- 16.Scenario:
- 17.Code-based:
- 18.What is the purpose of the admin.py file in a Django app?
- 19.Hari is creating a Django project with custom middleware.
- 20.Code-based:
- 21.Django Project Structure:
- 22.Django Project Directory Layout:
- 23.Django Apps and Project Configuration:
- 24.Settings.py Configuration:
- 25.Django Database Configuration:
- 26.Middleware Configuration:
- 27.Static and Media Files:
- 28.Environment Variables and Secret Management:
- 29.URL Configuration and URL Routing:
- 30.INSTALLED_APPS Configuration:
