Unexpected keyword argument in a view
Likely cause: The captured parameter name and function signature differ.
Check and correct: Match <str:name> to def greeting(request, name).
PROJECT 1 · LESSON 2 OF 16
Build About and a parameterized greeting using explicit URL configuration.
Continue from lesson 1: Install Django and serve Hello World. Keep existing files; create or replace the complete files listed below.
Extract the ZIP and open the folder containing manage.py. Create a Python virtual environment, install requirements.txt and run migrate. Read README.md for checkpoint-specific commands and LESSON.md for offline explanations. Checkpoints contain no database records or private credentials.
A store needs many addresses. URL routing gives each address a clear meaning and connects it to the function that handles it. Understanding this connection now makes product detail, edit and checkout routes much easier later.
Django routes are explicit Python mappings. Unlike Next.js, adding a folder does not create a URL. A view runs only after a matching pattern selects it.
The greeting demonstrates input from a URL. We will not use raw string interpolation to render untrusted HTML.
Django URL patterns are explicit Python declarations. Creating a file does not automatically create a route. config/urls.py is the entry point; include forwards the remaining path to shop/urls.py. This separates site-wide configuration from shop-specific addresses. Read the two URL files together when tracing a request.
A path converter such as <str:name> captures one path segment and passes it to the view using the same parameter name. The greeting view therefore receives request and name. A missing segment cannot satisfy the pattern, so Django continues checking other patterns and ultimately returns 404 if none match.
A path value identifies the resource or route: /hello/Asha/ selects a greeting for Asha. A query string such as ?q=book supplies optional input to a selected route. Django places query parameters in request.GET; it does not pass them as extra positional view arguments. Later /products/notebook/ identifies a product, while /products/?q=note filters a collection.
URL input is user-controlled. A path converter establishes a basic shape, not permission or business validity. An integer converter cannot prove that a task belongs to the visitor. A slug converter cannot prove that a product exists. Those checks belong in the view and database lookup.
A URL name is a stable application identifier. Templates can ask for shop:home rather than duplicate a literal address. app_name creates the shop namespace, so another app can also have a home or list route without ambiguity. Renaming the path then requires fewer edits.
Return an HttpResponse rather than a plain Python string. A response carries a body, status and headers. Our user-supplied greeting uses text/plain so a name containing angle brackets remains text. In later HTML templates, automatic escaping will handle ordinary variable output. Do not turn escaping off to make input look like HTML.
Read this sequence before the source. Identify the input, the component or function that handles it, and the result the learner sees.
Django reads the incoming path without its query string.
The root URLconf delegates to shop.urls.
The greeting pattern captures a name and calls the matching view.
The view constructs a plain-text response.
An unmatched address returns 404 instead of calling an unrelated function.
The paths below are relative to your practice project. Each block is the complete file at this checkpoint. Read its responsibility first, type or copy the code, then use the walkthrough to explain the important statements.
Declare the website’s three routes.
from django.urls import path
from . import views
app_name = "shop"
urlpatterns = [
path("", views.home, name="home"),
path("about/", views.about, name="about"),
path("hello/<str:name>/", views.greeting, name="greeting"),
]
Handle different requests with focused view functions.
from django.http import HttpResponse
def home(request):
return HttpResponse("<h1>Hello, Django!</h1><p>My first working website.</p>")
def about(request):
return HttpResponse("<h1>About EduShop</h1><p>Practical supplies for learners.</p>")
def greeting(request, name):
return HttpResponse(f"Hello, {name}!", content_type="text/plain")
Change a route path while keeping its name, then visit the old address.
The old address returns 404 unless another pattern matches it.
A route name helps reverse URL generation; it does not automatically create redirects from earlier addresses.
A route argument and its view parameter must have matching names. A mismatch produces an unexpected-keyword error.
Likely cause: The captured parameter name and function signature differ.
Check and correct: Match <str:name> to def greeting(request, name).
Likely cause: The app URLconf is not included or the path differs.
Check and correct: Trace config/urls.py to shop/urls.py and check trailing slashes and prefixes.
Add /help/ and give it the route name help.
Attempt these questions before opening the explanations. Use the source above to justify your answer.
No. It only extracts input. You must explicitly query a model when the feature requires stored data.
The project chooses where apps are mounted. Each app owns its internal routes, which keeps later task and order routes understandable.