N
N
nurzhannogerbek2017-03-28 00:18:05
Django
nurzhannogerbek, 2017-03-28 00:18:05

Display a message about an existing similar record?

Hello!

There is a modal window for creating a new entry, where AJAX is used. The modal window itself has only one field (name). Whether I try before saving to check up there is the same record. If so, display the message in this modal form. What is the correct way to display such messages? Below is an example of a view function for adding a new entry.

view.py :

def function_add(request, project_code):
    data = dict()
    project = get_object_or_404(Project, pk=project_code)
    if request.method == 'POST':
        form = FunctionAddForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            if Function.objects.filter(name=name, project=project_code).exists():
                # Cообщение об ошибке 'Already exist'
            else:
                function = form.save(commit=False)
                function.project = project
                function.save()
            data['form_is_valid'] = True
            functions = Function.objects.filter(project=project_code)
            data['html_function'] = render_to_string('project/function_list.html', {'functions': functions})
        else:
            data['form_is_valid'] = False
    else:
        form = FunctionAddForm()
    context = {'project': project, 'form': form}
    data['html_function_add_form'] = render_to_string('project/function_add.html', context, request=request)
    return JsonResponse(data)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question