Django add success message erro
Whenever i am using messages I keep getting the same type error saying string is no callable. However this same code used to work, I am not sure what happened but it just stopped working and started giving me this error for all messages in all pages, even when i add something on admin panel. Not sure what is causing this error. Please help.
Request Method: POST
Request URL: http://127.0.0.1:8000/compras/fornecedores/
Django Version: 5.1.3
Exception Type: TypeError
Exception Value:
'str' object is not callable
Exception Location: /Users/macbook-fcorrea/Documents/Coding/cabiunas/compras/views/views.py, line 122, in fornecedores
Raised during: compras.views.views.fornecedores
Python Executable: /Users/macbook-fcorrea/Documents/Coding/cabiunas/cabiunas-env/bin/python3
Python Version: 3.13.1
Python Path:
['/Users/macbook-fcorrea/Documents/Coding/cabiunas',
'/opt/homebrew/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python313.zip',
'/opt/homebrew/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13',
'/opt/homebrew/Cellar/python@3.13/3.13.1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/lib-dynload',
'/Users/macbook-fcorrea/Documents/Coding/cabiunas/cabiunas-env/lib/python3.13/site-packages',
'/Users/macbook-fcorrea/Documents/Coding/cabiunas/cabiunas-env/lib/python3.13/site-packages/setuptools/_vendor']
This is my views.py
def fornecedores(request):
form_empty = NewFornecedor()
# FILTER
fornecedor_filter = FornecedorFilter(
request.GET, queryset=Fornecedor.objects.all().order_by('name'))
all_forn = fornecedor_filter.qs
if request.method == 'GET':
context = {
'all_forn': all_forn,
'fornecedorform': form_empty,
'filter': fornecedor_filter.form
}
if request.htmx:
return render(request, "compras/partials/fornecedor-lista.html", context)
return render(request, "compras/fornecedores.html", context)
elif request.method == 'POST':
form = NewFornecedor(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Fornecedor adicionado com sucesso.')
return redirect("compras:fornecedores")
else:
print(form.errors)
return render(request, "compras/fornecedores.html", {'form': form, 'all_forn': all_forn, 'filter': fornecedor_filter.form})
This is my template: fornecedores.html
{% extends "compras/layout.html" %}
{% load crispy_forms_tags %}
{% load static %}
{% block body %}
<div class="mt-5">
{% if messages %}
{% for message in messages %}
<div class="alert {% if message.tags == 'error' %}alert-danger {%else%}alert-{{ message.tags }}{% endif %} mt-1"
role="alert">
{{message}}
</div>
{% endfor %}
{% endif %}
<div class="row">
<div class="col-12 mb-3 d-flex align-items-center">
<h1>Fornecedores</h1>
<div class="ml-3">
<button class="btn btn-primary btn-pink" type="button" data-toggle="collapse"
data-target="#addFornecedor" aria-expanded="false" aria-controls="addFornecedor">
Adicionar Fornecedor
</button>
</div>
</div>
</div>
<div class=" mt-2 mb-4 collapse" id="addFornecedor">
<div class="card ">
<h5 class=" card-header">Novo Fornecedor</h5>
<div class="card-body ">
<form id="form-fornecedor" action="{% url 'compras:fornecedores' %}" method="post">
{% csrf_token %}
<div class="row pl-3 mb-3">
{% crispy fornecedorform %}
<div class="form-group">
<input type="submit" id="btn-novo-fornecedor" value="Adicionar"
class="btn btn-sm btn-primary" />
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% include 'compras/partials/fornecedor-lista.html' %}
<section id="messages">
{% include 'compras/partials/message.html'%}
</section>
{% endblock %}
This is what shows on my terminal window:
Internal Server Error: /compras/fornecedores/
Traceback (most recent call last):
File "/Users/macbook-fcorrea/Documents/Coding/cabiunas/cabiunas-env/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/Users/macbook-fcorrea/Documents/Coding/cabiunas/cabiunas-env/lib/python3.13/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/macbook-fcorrea/Documents/Coding/cabiunas/cabiunas-env/lib/python3.13/site-packages/django/contrib/auth/decorators.py", line 60, in _view_wrapper
return view_func(request, *args, **kwargs)
File "/Users/macbook-fcorrea/Documents/Coding/cabiunas/compras/views/views.py", line 122, in fornecedores
messages.success(request, 'Fornecedor adicionado com sucesso.')
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'str' object is not callable
Thank you!