vsupalov

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.

Want to create better Django apps?

Subscribe for regular articles about Django!

     (About the content, privacy, analytics and revocation). 

    We won't send you spam. Unsubscribe at any time.

    Powered By ConvertKit