Skip to content
Advertisement

Django: Could not parse the remainder

I’m trying to pass a response from JsonResponse as a parameter of specific function in views. But, got the following error instead

django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '${response.id_ajuan}' from '${response.id_ajuan}'

Here it is my code

url.py

url(r'^course-eksternal/review/(?P<id>d+)/update$', course_eksternal.update_ajuan, name='update-ajuan')

views.py

# Function that pass the JsonResponse
def get_update_ajuan(request):
    ajuan = Pengajuan.objects.get(pk=request.POST.get('ajuan_id'))
    res = {
        'id_ajuan': ajuan.id,
        ...
    }
    status_code = 200
    return JsonResponse(res, status=status_code)

file.html

# Get the JsonResponse
success : function(response) {
    $('.modal-body').empty().append(`
        <div class="modal-body">
            <form action="{% url 'app:update-ajuan' id=${response.id_ajuan} %}" method="POST"> # Error occurs here
                ...
`);

Advertisement

Answer

Your success function is javascript that is running in the front-end. Django is not running your front-end and there is no processing of django templates going on here, so you can’t use any django template tags.

What you will need to do is figure out what the url is in your view get_update_ajuan and then pass the whole url back.

from django.urls import reverse

def get_update_ajuan(request):
    ajuan = Pengajuan.objects.get(pk=request.POST.get('ajuan_id'))
    res = {
        'id_ajuan': ajuan.id,
        'action_url': reverse("app:update-ajuan", kwargs={"id": ajuan.id},
        ...
    }
    status_code = 200
    return JsonResponse(res, status=status_code)

and then use that value:

success : function(response) {
    $('.modal-body').empty().append(`
        <div class="modal-body">
            <form action="${response.action_url}" method="POST"> # Error occurs here
                ...
`);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement