close
close

python – Django just sends an alert in response instead of a full model

python – Django just sends an alert in response instead of a full model

How can I simply send an alert message in response to a request instead of having to send a template created just for the alert?

I am using Javascript asynchronous call. I only need the alert HTML response to render a InnerHTML.

I’m also using Bootstrap. I need the post type so Bootstrap can render the correct color/CSS.

Settings.py with Bootstrap alert types.

MESSAGE_TAGS = {
        messages.DEBUG: 'alert-secondary',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'alert-success',
        messages.WARNING: 'alert-warning',
}

See

@login_required(login_url="/login/")
@csrf_protect
def usersave(request):
msg = messages.add_message(request, messages.WARNING, "This is a demo. First complete all info to save.")
return render(request, msg)   # Want to send just msg instead of a template for an alert. 

Model

{% if messages %}
    <div class="container text-center my-3">
        {% for message in messages %}
                <div class="alert {{ message.tags }} alert-dismissible fade show" role="alert">
                    <span id="alertmessage">{{ message }}</span>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
        {% endfor %}
    </div>
{% endif %} 

python – Django just sends an alert in response instead of a full model

python – Django just sends an alert in response instead of a full model

How can I simply send an alert message in response to a request instead of having to send a template created just for the alert?

I am using Javascript asynchronous call. I only need the alert HTML response to render a InnerHTML.

I’m also using Bootstrap. I need the post type so Bootstrap can render the correct color/CSS.

Settings.py with Bootstrap alert types.

MESSAGE_TAGS = {
        messages.DEBUG: 'alert-secondary',
        messages.INFO: 'alert-info',
        messages.SUCCESS: 'alert-success',
        messages.WARNING: 'alert-warning',
}

See

@login_required(login_url="/login/")
@csrf_protect
def usersave(request):
msg = messages.add_message(request, messages.WARNING, "This is a demo. First complete all info to save.")
return render(request, msg)   # Want to send just msg instead of a template for an alert. 

Model

{% if messages %}
    <div class="container text-center my-3">
        {% for message in messages %}
                <div class="alert {{ message.tags }} alert-dismissible fade show" role="alert">
                    <span id="alertmessage">{{ message }}</span>
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                </div>
        {% endfor %}
    </div>
{% endif %}