Skip to content

Register a View

Overview

Render your template containing your ReactPy component using a Django view.

Note

We assume you have created a Django View before, but we have included a simple example below.


Creating a Django view and URL path

Within your Django app's views.py file, you will need to create a function to render the HTML template containing your ReactPy components.

In this example, we will create a view that renders my-template.html (from the previous step).

1
2
3
4
5
from django.shortcuts import render


def index(request):
    return render(request, "my-template.html")

We will add this new view into your urls.py.

1
2
3
4
5
6
from django.urls import path
from example import views

urlpatterns = [
    path("example/", views.index),
]
Which urls.py do I add my views to?

For simple Django projects, you can easily add all of your views directly into the Django project's urls.py. However, as you start increase your project's complexity you might end up with way too much within one file.

Once you reach that point, we recommend creating an individual urls.py within each of your Django apps.

Then, within your Django project's urls.py you will use Django's include function to link it all together.


Last update: August 26, 2023
Authors: Mark Bakhit