I have the below urlpatterns
in the root's url.py
:
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^api/', include('api.urls'), name="api_app"),
]
where api
app contains:
app_name="api_app"
urlpatterns = [
url(r'^users/', views.UserList.as_view()),
url(r'^users/(?P<user_type>[a-zA-Z_]+)$', views.UserList.as_view(), name="filter_type"),
...,
]
the first url
displays a list of users and the second url
accepts a user type and filters the user list with the user_type
These both work fine when I put the urls in a browser's address bar. However when I try to reference the second url from a django template like so:
<form action="{% url "api_app:filter_type" user_type %}" method="GET">
<select name="user_type" class="userType">
<option value="none">Select user type</option>
<option value="A">A/option>
<option value="B">B</option>
<option value="C">C</option>
</select>
<input type="submit" value="Submit">
</form>
The below error occurs:
NoReverseMatch at /api/users/
Reverse for 'filter_type' with arguments '('',)' not found. 1 pattern(s) tried: ['api/users/(?P<user_type>[a-zA-Z_]+)$']
why is that? aren't the namespaces configured correctly?
The problem apparently is that user_type
is not defined anywhere which I understand. But with the above <Select>
tag how can I define user_type
to be the selected option in html?
update
This is my view that I want to pass the user_type
data to:
class UserList(APIView):
renderer_classes = [TemplateHTMLRenderer]
def get(self, request, user_type=None):
filters = {}
if user_type:
filters['user_type'] = user_type
users = User.objects.filter(**filters)
serializer = UserSerializer(users, many=True)
return Response({"data": json.dumps(serializer.data)}, template_name="users.html")