How to configure Django Dynamic Menu with the URLs.py

My app is generating a list that would be the side menu, like this:

Views.py:

def generate_menu(request):
array = []
deviceList = []
    
    f1 = open('dictionary.txt', 'r')
    array = f1.read()
    array = literal_eval(array)
    for i in sorted(array):
        deviceList.append(i)
    f1.close()
    context.update({'deviceList': deviceList})

return render(request, "device_list.html", context)

Now in my Template, I am doing this to create the menu based on dic file:

device_list.html:

<nav id="sidebar">
    <ul class="list-unstyled components">
        <li class="active">
            <a href="device_list">Device List</a>
        </li>
        {% block devices %}
        {% for each in deviceList %}
        <li>
            <form action="/{{each}}" method="POST">
                {% csrf_token %}
                {{ form.as_p }}
                <button type="submit" value={{each}} name='DEVICES'>{{each}}</button>

            </form>   
        </li>
        {% endfor %}
        {% endblock %}
    </ul>
</nav>

URLs.py:

url(r'^$', app.views.index),
url(r'^(?P<string>.+)$', app.views.generate_menu, name='DEVICES'),

and it doesn't work here. So how can I change the URL in a way to accept all kind of request like http://127.0.0.1/Device* because all device list names, are different and they need their own url. So I need to have a url path that is dynamic.

Back to Top