Forums

Dropdown works locally but not in Pythonanywhere

Hi, I am using Django. In the view I have a QuerySet that I pass to the template. The QuerySet-items are used as items of a dropdown menu (of the Materialize-Framework). It works when I test locally, but when I pull the code to Pythonanywhere, the dropdown doesn't open. The code looks like this:

views.py

def bicycle_list(request):
    manufacturers = Bicycle.objects.values_list('handlebar', flat=True).distinct()
    return render(request, 'shop/bicycle_list.html', {'manufacturers': manufacturers})

bicycle_list.html

....
<head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
   <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
   <script>
        $(document).ready(function(){
                    $('.dropdown-trigger').dropdown();
         });
   </script>
   <meta name = "viewport" content = "width = device-width, initial-scale = 1">
   <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
   <link rel ="stylesheet" href = "https://fonts.googleapis.com/icon?family=Material+Icons">
</head>
<body>
       .....
      <ul id='dropdown1' class='dropdown-content'
       {% for manufacturer in manufacturers %}
            <li><a href="#!">{{ manufacturer }}</a></li>
       {% endfor %}
      </ul>
       ....
</body>

Is there any idea what might be the problem?

You can use the network and console tabs of the developer tools in your browser to see whether something is failing to load or whether you're getting a javascript error and use that information to start debugging.

Thanks for the advice. I looked at the developer tools, but there are no error messages: All the stylesheets/scripts were loaded. And if I click on the dropdown menu, nothing happens and no error message occurs.

When I worked locally, it was important to first load the js-script before the css:

https://stackoverflow.com/a/62302413

Is it possible that Pythonanywhere loads in a different order?

Could you give a URL for a page on your site that shows the problem?

Yes, you can find it here: [removed the link] The button "Nach Bezirk filtern" is the dropdown.

Thanks a lot in advance!

I think it might not be showing up as a dropdown because it has no options -- when I look at the page source for that page I see this:

<a class='dropdown-trigger btn-large' href='#' data-target='dropdown1'>NACH BEZIRK FILTERN</a>
 <ul id='dropdown1' class='dropdown-content'>

 </ul>

You are right. When I looked it up yesterday I saw the same. Now, one day later without changing anything, the code somehow decided to work. However, thanks a lot for your advice.

for me also the dropdown doesn't seem to function in pythonanywhere, but it works perfectly in local system. The link to the project is given below. Please suggest changes http://harisoorya12.pythonanywhere.com/

It's probably because your bootstrap.js script isnt being loaded. Check out the network tab of your browsers devtools