django-silk icon indicating copy to clipboard operation
django-silk copied to clipboard

Cleardb view context error

Open panhaoyu opened this issue 3 years ago • 1 comments

Here's some code in clear_db.html:

{% block data %}
    <div class="wrapper">
        <div class="inner">
            <h2>Silk Clear DB</h2>
            <form class="cleardb-form" action="." method="post">
                {% csrf_token %}
                <div class="cleardb-form-wrapper">
                    <label>
                        <input type="checkbox" name="clear_requests" disabled="disabled"/>
                        Requests
                    </label>
                    <label>
                        <input type="checkbox" name="clear_profiling" disabled="disabled"/>
                        Profiling
                    </label>
                    <label>
                        <input type="checkbox" name="clear_all"/>
                        All
                    </label>
                </div>
                <button class="btn">Clear</button>
            </form>
            <div class="msg">{{ msg }}</div>
        </div>
    </div>
{% endblock %}

Here's the code of view:

class ClearDBView(View):

    @method_decorator(login_possibly_required)
    @method_decorator(permissions_possibly_required)
    def get(self, request, *_, **kwargs):
        return render(request, 'silk/clear_db.html')

    @method_decorator(login_possibly_required)
    @method_decorator(permissions_possibly_required)
    def post(self, request, *_, **kwargs):
        context = {}
        if 'clear_all' in request.POST:
            delete_model(Profile)
            delete_model(SQLQuery)
            delete_model(Response)
            delete_model(Request)
            tables = ['Response', 'SQLQuery', 'Profile', 'Request']
            context['msg'] = 'Cleared data for following silk tables: {0}'.format(', '.join(tables))
        return render(request, 'silk/clear_db.html', context=context)

The important lines are:

            <div class="msg">{{ msg }}</div>
    def get(self, request, *_, **kwargs):
        return render(request, 'silk/clear_db.html')
    def post(self, request, *_, **kwargs):
            context['msg'] = 'Cleared data for following silk tables: {0}'.format(', '.join(tables))

We can see, only post have msg variable but both get and post need it.

I wanted to send a PR, but the project is unable to run on my windows device, so just record it here, hope to fix it.

panhaoyu avatar May 22 '21 05:05 panhaoyu

Can you paste the full traceback of your error? It should not be an error if the variable is inexistent

SebCorbin avatar Oct 27 '22 12:10 SebCorbin