0

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")
4

2 回答 2

0

问题不在于命名空间;是参数。您正在传递user_type,但它要么是空的,要么(更有可能)未定义您使用它的位置,因此它与预期的参数不匹配。

编辑你很困惑。您正在/api/users进入浏览器并转到该页面。在该页面上,您有一个{% url "api_app:filter_type" user_type %}尝试生成链接的调用;但是user_type是空的,所以不能工作。

于 2019-10-17T07:28:46.803 回答
0

您已经像这样定义了您的网址

url(r'^users/(?P<user_type>[a-zA-Z_]+)$', views.UserList.as_view(), name="filter_type")

因此,当您对 url 进行反向查找时,它需要与正则表达式模式匹配的参数[a-zA-Z_]+

查看您的代码,您正在传递user_type给 url

<form action="{% url "api_app:filter_type" user_type %}" method="GET"></form>

但是,它抛出了这个错误

NoReverseMatch at /api/users/
Reverse for 'filter_type' with arguments '('',)' not found. 1 pattern(s) tried: ['api/users/(?P<user_type>[a-zA-Z_]+)$']

这意味着user_type要么是空字符串,要么是未定义的。user_type需要是与模式匹配的字符串,[a-zA-Z_]+以便您能够对 url 执行反向匹配。

编辑 这就是我使用视图的方式

<form action="view_user_type" method="POST">
    <select name="user_type" class="userType">
        <option selected="selected" disabled>Select user type</option>
        <option value="A">A/option>
        <option value="B">B</option>
        <option value="C">C</option>
    </select>
    <input type="submit" value="Select"
</form>


# forms.py

class UserTypeForm(forms.Form):
    USER_TYPE_CHOICES = (
        ('A', 'A'),
        ('B', 'B'),
        ('C', 'C'),
    )

    user_type = forms.ChoiceField(choices = USER_TYPE_CHOICES)


# views.py

from django.core.urlresolvers import reverse
from .forms import UserTypeForm

def view_user_type(request):
    if request.method == 'POST':
        form = UserTypeForm(request.POST)
        if form.is_valid():
            user_type = form.cleaned_data.get('value')
            return redirect(reverse('api_app:filter_type', args=(user_type,)))
于 2019-10-17T08:31:38.613 回答