0

So I have probably the simplest task you could ever wish for with an html form that I cannot solve.

I want to input a brand name into a search bar, click submit and it redirect to the url/brand/[input]

I already have a working view for the urls setup in django. How would I structure the form to actually create my desired url?

  <form method="GET" action="">
     <input type="text" name="brand" id="brand" placeholder="Search..." aria-label="Search">
     <button type="submit">Search</button>
  </form>

views.py

class GetByBrand(ListView):
    def get(self, request, brand):

urls.py

urlpatterns = [
    path('', GetListView.as_view(), name='home'),
    path('brands/<str:brand>/', GetByBrand.as_view())
]
1
  • show Your views.py and urls.py Commented Jul 9, 2020 at 18:32

3 Answers 3

0
 <form method="GET" action="brands/">
 <input type="text" name="brand" id="brand" placeholder="Search..." aria-label="Search">
 <button type="submit">Search</button>

in URLs(import views by from. import views)

urlpatterns = [
path('brands/', views.GetByBrand),
] 

in Views from django.http import HttpResponse def GetByBrand(request): s = request.GET['brand'] return HttpResponse(s)

Sign up to request clarification or add additional context in comments.

Comments

0

OR use AJAX in Javascript in your HTML file

$('#brand').on('submit',function(e){
 var brand = $('input[name=brands]')
 $.get("brands/"+brand, function(data, status){
 alert("Data: " + data + "\nStatus: " + status);

}); })

Comments

0

So I basically solved this by using the code I had previously tried which is identical to what Wagas Develper recommended. What I had not done though was add this last line to the urls.py in the main project folder. (and I'm still not sure why this solved it but found the solution from throwing crap at the wall)

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('shoes.urls')),
    path('brands/', include('shoes.urls'))

For anyone curious about how the class based view is structured now.

class GetByBrand(ListView):

    def get(self, request, brand):

        get_results = request.GET.get('q')

The urls.py in the same directory as views.py (within app dir not main project dir)

urlpatterns = [
    path('', GetListView.as_view(), name='home'),
    path('brands/<str:brand>/', GetByBrand.as_view()),
    path('brand_results/', GetByBrand.as_view(), name='b')
]

HTML Form

  <form method="GET" class="form-inline my-2 my-lg-0" action="{% url 'b' %}">
    <input class="form-control mr-sm-2 search-btn" type="text" name="q" value="{{request.GET.q}}" placeholder="Search..." aria-label="Search">
    <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
  </form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.