Serghey Hmeli
Serghey Hmeli

Reputation: 63

How to change url in Django

I am new to Django and maybe formulation of the question is not the best, so sorry for that. I am trying to implement a search functionality and it seems to work fine, except it is not matching the path.

I want it to look like this after I click submit button: search/query=value.

But I get this instead: /ru/”/ru/search/”?csrfmiddlewaretoken=SSoBp5K7E0EgRFQNyIvECSXFohG5ACp9IKNGKXMOYNmdc8BqqHeKLR8vawHuVxwf&”txtSearch”=1.

I am using i18n to prefix paths with language.

Here is code that relevant to the problem:

urls.py

urlpatterns = [
    path('', HomeView.as_view(), name='home'),
    path('search/', search, name='search'),
]

home.html

<form id=”search” method=”GET” action=”{% url 'search' %}”>
    <input type=”text” id=”txtSearch” name=”txtSearch”>
    <button type=”submit”>Submit</button>
</form>

views.py

def search(request):
    if request.method == 'GET':
        query = request.GET['”txtSearch”']
        queryset = []
        queries = query.split(' ')
        for q in queries:
            articles = Article.objects.filter(
                Q(name__icontains=q) |
                Q(body__icontains=q)
            ).distinct()
            
            for article in articles:
                queryset.append(article)

    return render(request, 'search.html', {'articles': list(set(queryset))})

Thank you!

Upvotes: 0

Views: 160

Answers (1)

babak gholamirad
babak gholamirad

Reputation: 568

try this:

<form id=”search” method=”GET” action="search">
    <input type=”text” id=”txtSearch” name=”txtSearch”>
    <button type=”submit”>Submit</button>
</form>

Upvotes: 1

Related Questions