django search page not found error

65 views
Skip to first unread message

omid jahadi

unread,
Mar 24, 2020, 8:03:22 PM3/24/20
to Django users
Hello everybody! I want to search in a ManyToManyField in the DetailView. It works fine if a user with the same query exist, but if there isn't a user, i get page not found error.

models.py:

class agents(models.Model):
    agent_type = models.ForeignKey(types, on_delete=models.SET_NULL, blank=True, null=True)
    name = models.CharField(max_length=100)
    users = models.ManyToManyField(user_models.users, through='user_agent')

views.py:

class AgentDetailView(LoginRequiredMixin, generic.DetailView):
    model = models.agents
    template_name = 'agent/agent_detail.html'

    def get_queryset(self):
        query = self.request.GET.get('q')
        if query:
            return models.agents.objects.filter(Q(users__user__first_name__contains=query)
                                                | Q(users__user__last_name__contains=query)
                                                | Q(users__id_number__contains=query)
                                                | Q(users__mobile__contains=query))
        else:
            return models.agents.objects.all()

agent_detail.html:

<h1>name: {{ agents.name }}</h1>
    <form method="GET" action="" id="searchform">
        <input class="searchfield" id="searchbox" name="q" type="text" placeholder="Search..."/>
        <button name="search" type="submit" value="{{ request.GET.q }}">Search</button>
    </form>
    {% if agents.users %}
        <p><strong>Users:</strong>
            <br>
            {% for users in agents.users.all %}
                <li>{{ users }}</li>
                <hr>
            {% endfor %}
        </p>
    {% else %}
        <p>There are no user for this agent in database.</p>
    {% endif %}

Motaz Hejaze

unread,
Mar 24, 2020, 10:30:00 PM3/24/20
to django...@googlegroups.com
Add null=True to manytomany field


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/54061fa4-2412-424a-8887-916dcc10c051%40googlegroups.com.

omid jahadi

unread,
Mar 25, 2020, 6:46:03 AM3/25/20
to Django users
It doesn't work ... Actually, ManyToManyField is not null ... I get error when i search a keyword that doesn't match with any user! ... For example, first_name is 'Omid', when i search 'o' or 'm', search works fine and return 'Omid', but, when i search 'k', i get PageNotFound error
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Motaz Hejaze

unread,
Mar 25, 2020, 3:38:14 PM3/25/20
to django...@googlegroups.com
No problem , you can catch this exception in your views.py if you want ..

To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/703c8b77-9775-4895-b5cd-a701f7e2d69d%40googlegroups.com.

omid jahadi

unread,
Mar 26, 2020, 6:23:51 AM3/26/20
to Django users
How can i handle this exception ... I have this error: "No agents found matching the query"
Reply all
Reply to author
Forward
0 new messages