0

Django is very challenging and I still need to get used to the code and currently, I just want the search bar to display every time a user input a text and it will display like a title I really don't how to interpret the code to tell that every time the user inputs in the search bar, It is supposed to display the user input on a page like a title.

Example: user input in the search bar: cat and it displays cat title

Display on the current page:

Result search: "Cat"

HTML Code

<!-- Search Bar -->
<form action="{% url 'enc:search' %}" method="GET">
  {% csrf_token %}
  <input class="search" type="text" name="q" placeholder="Search">
</form>

In my views.py I only write this code and I don't know what to write it.

views.py

def search (request):
    title = request.GET.get("q", "")

urls.py

urlpatterns = [
    path("", views.index, name="index"),
    path("search/", views.search, name="search"),

Right now just a simple display from the search bar input later I will code it in a data search where there is some file to integrate the search but right now I really need some help my brain is cracking. It's kinda sad I mean I know it be a simple code but I don't know why I can't figure it out.

please do help me if you have tips on how to be better on Django and python too it be much help for me and thank you for your time I really appreciate it very much.

NISHA NAJIHAH
  • 177
  • 2
  • 12
  • maybe you get into a course. get some hands-on time and things get better ! – SANGEETH SUBRAMONIAM Apr 02 '21 at 04:22
  • thank you @SANGEETHSUBRAMONIAM. Do you have any good course recommendations and some websites that I can get something to do with some hands-on project or any task or question it is good too? I will keep on trying :) – NISHA NAJIHAH Apr 02 '21 at 04:30
  • There are plenty of courses . Fortunately Django has a very huge active community. I cannot recommend a course and say this is enough, unless it is the official documentation. But as a beginner, I would suggest you try out different courses, get your hands dirty and find which learning method suits you... I started with Dennis Ivy's django bootcamp free on youtube. begginer friendly... then went with some udemy courses... Remember just like any other learning, the thing takes some time to figure out what is happening around and once you grab the basics..., things get interesting.. GoodLuck! – SANGEETH SUBRAMONIAM Apr 02 '21 at 04:52

1 Answers1

2

searchpage.html :

<!-- Search Bar -->
<form action="{% url 'enc:search' %}" method="POST">
  {% csrf_token %}
  <input class="search" type="text" name="q" placeholder="Search">
  <input type="submit" value="Submit">
</form> 

views.py:

from django.shortcuts import render

def search (request):
    #defines what happens when there is a POST request
    if request.method == "POST":
        title = request.POST.get("q")
        return render(request,'new_template.html', { 'title' : title })


    #defines what happens when there is a GET request
    else:
        return render(request,'searchpage.html')

new_template.html:

<!DOCTYPE html>
{% load static %}
<html>
<head>
<title>search term</title>
</head>
<body>

<h1> {{ title }} </h1>

</body>
</html>
SANGEETH SUBRAMONIAM
  • 1,098
  • 1
  • 9
  • 10
  • Thank you @SANGEETHSUBRAMONIAM. It is a simple code sad :(. but I think I don't get on how to use the POST and GET part. How do you know when to use get or post sometimes the circumstance seems like GET but here you use POST? – NISHA NAJIHAH Apr 02 '21 at 04:36
  • POST when sending data to the backend like submitting a form, updating databases etc... GET when you need to pull something from the backend, like when just rendering a page – SANGEETH SUBRAMONIAM Apr 02 '21 at 04:45
  • Oh I see so using post is to tell the code that the user input some data and when you want to display the search you have to tell the code GET the data. Is this right? – NISHA NAJIHAH Apr 02 '21 at 04:47
  • yes. Just like how it spells ... POST - posts data ... GET - gets data – SANGEETH SUBRAMONIAM Apr 02 '21 at 04:53
  • 1
    I see okay I think I get the picture yes. Let me practice some more on the get and post. Thank you so much @SANGEETHSUBRAMONIAM – NISHA NAJIHAH Apr 02 '21 at 06:42