Snippet: The Most Simple URL-Based View in Django

When starting a new Django project, I always like to get a first, simple view working.

It’s kind of a “Hello World” step I like to take.

If you have created and configured a templates folder, and created your first template within it, all that’s left to do is to add two lines to the project’s urls.py:

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

from django.views.generic.base import TemplateView # here

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', TemplateView.as_view(template_name="base.html")), # and here
]

This will render the base.html template when the root of your new project is visited. A first template-rendering class-based view.

Subscribe to my newsletter!
You'll get notified via e-mail when new articles are published. I mostly write about Docker, Kubernetes, automation and building stuff on the web. Sometimes other topics sneak in as well.

Your e-mail address will be used to send out summary emails about new articles, at most weekly. You can unsubscribe from the newsletter at any time.

Für den Versand unserer Newsletter nutzen wir rapidmail. Mit Ihrer Anmeldung stimmen Sie zu, dass die eingegebenen Daten an rapidmail übermittelt werden. Beachten Sie bitte auch die AGB und Datenschutzbestimmungen .

vsupalov.com

© 2024 vsupalov.com. All rights reserved.