Before we dive into creating our first URL and view, let's clarify some key concepts:

Route: In Django, a route leads us to a specific resource by creating the path part of a URL. This routing is defined in the urls.py file(s) located in the Django project root directory and/or inside application directory(s).

View: A view is the logic that handles a request and returns a response. In Django, views are typically defined as functions or classes within the views.py file of an application directory. Views contain the actions and business logic that are executed when a URL is accessed.

Step-By-Step Setup Of Our First URL http://example.com/challenges/january

Let's create our first URL and view in Django. We'll set up a simple "January challenge" page.

1. Create the View

First, let's create our resource. Open the views.py file in your app directory (let's assume we have an app called 'challenges') and add the following code:

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello, this is the January challenge!")

This view function does two things:

  • It accepts a request object, which is automatically generated by Django when a user accesses the URL.
  • It returns a simple HTTP response with our message.

NOTE: In this basic example, we're not doing anything with the request object. We're just accepting it and returning a response.

2. Create the URL Pattern

Next, we need to map this view to a URL. Create a new file called urls.py in your app directory (if it doesn't exist already) and add the following:

from django.urls import path
from . import views

urlpatterns = [
    path("january", views.index),
]

This creates a URL pattern that maps the "january" path to our index view.

3. Include App URLs in Project URLs

Finally, we need to make the project aware of our app's URLs. Open the urls.py file in your project's main directory and update it like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path("challenges/", include("challenges.urls")),
]

This includes the URLs from our 'challenges' app under the "challenges/" path.

Now, when you run your server and navigate to http://localhost:8000/challenges/january, you should see the message "Hello, this is the January challenge!".

Understanding the Flow:

  1. User visits http://localhost:8000/challenges/january
  2. Django checks the main urls.py and sees that "challenges/" should use the URLs from the challenges app
  3. It then checks the app's urls.py and finds the "january" pattern
  4. This pattern points to the index view in views.py
  5. The view returns an HttpResponse, which Django sends back to the user's browser

This is a basic example, but it demonstrates the fundamental flow of URLs and views in Django.

Related Web Basics Articles:

These related articles will help you build a more comprehensive understanding of web development with Django. Each topic expands on the concepts introduced here and will help you create more complex and feature-rich applications.