Django Forms with ReactJS

First, i think you need to check related React documentation about forms with multiple inputs. It gives you base idea about how things should be structured in React side.

About fetching data from server, you can try something like this in componentDidMount:

componentDidMount() {
    // Assuming you are using jQuery,
    // if not, try fetch().
    // Note that 2 is hardcoded, get your user id from 
    // URL or session or somewhere else.
    $.get('/api/profile/2/', (data) => {
            formFields: data.fields // fields is an array

Then you can create your html input elements in render method with something like this:

render () {
    let fields = => {
        return (
            <input type="text" value={field.value} onChange={(newValue) => {/* update your  state here with new value */ }} name={}/>
    return (
        <div className="container">
            <form action="">
                <button onClick={this.submitForm.bind(this)}>Save</button>

And here is your submitForm method:

submitForm() {
    $.post('/api/profile/2/', {/* put your updated fields' data here */}, (response) => {
       // check if things done successfully.


Here is an untested-but-should-work example for your DRF view:

from rest_framework.decorators import api_view
from django.http import JsonResponse
from rest_framework.views import APIView

class ProfileFormView(APIView):
    # Assume you have a model named UserProfile
    # And a serializer for that model named UserSerializer
    # This is the view to let users update their profile info.
    # Like E-Mail, Birth Date etc.

    def get_object(self, pk):
            return UserProfile.objects.get(pk=pk)
            return None

    # this method will be called when your request is GET
    # we will use this to fetch field names and values while creating our form on React side
    def get(self, request, pk, format=None):
        user = self.get_object(pk)
        if not user:
            return JsonResponse({'status': 0, 'message': 'User with this id not found'})

        # You have a serializer that you specified which fields should be available in fo
        serializer = UserSerializer(user)
        # And here we send it those fields to our react component as json
        # Check this json data on React side, parse it, render it as form.
        return JsonResponse(, safe=False)

    # this method will be used when user try to update or save their profile
    # for POST requests.
    def post(self, request, pk, format=None):
            user = self.get_object(pk)
            return JsonResponse({'status': 0, 'message': 'User with this id not found'})

        e_mail ="email", None)
        birth_date ='birth_date', None)
        job ='job', None) = e_mail
        user.birth_date = birth_date
        user.job = job

            return JsonResponse({'status': 1, 'message': 'Your profile updated successfully!'})
            return JsonResponse({'status': 0, 'message': 'There was something wrong while updating your profile.'})

And this is related url for this view:

urlpatterns = [
    url(r'^api/profile/(?P<pk>[0-9]+)/$', ProfileFormView.as_view()),

Leave a Comment