Val
i got this error🙈
inchidi
i cant see the messages, can you make it bigger?
inchidi
i mean taller
Val
i hope this is better
inchidi
you still using this code at your views.py?
Val
you still using this code at your views.py?
yes. should i change it to your own?
Val
okay
Val
it loaded finally
Val
inchidi
nice, the fix for your views code like this: return HttpResponse( template.render({}, request)) but using render shortcut is simpler you can read the different here https://stackoverflow.com/a/7303168/3925477
inchidi
your welcome 🙈
Val
i am trying to use django to solve that task its a school project is it wise i use django for it?
Val
should be no problem
now i have created the search_term input, you have any idea what next i should do?
Val
should be no problem
how do i post a method on the same controller that displayed the index page
inchidi
now i have created the search_term input, you have any idea what next i should do?
you need to prepare your index to receive GET value from user
inchidi
pls how do i do that?
def index(request): if request.method == 'GET': return render(request, 'application/index.html',{'data':request.GET['query']}) return render(request, 'application/index.html')
inchidi
your views.py already at "application" app directory
inchidi
def index(request): if request.method == 'GET': return render(request, 'application/index.html',{'data':request.GET['query']}) return render(request, 'application/index.html')
your index will pass input query from user to the template and you can get it using {{ data }} inside your template (index.html)
Val
i got this error?
inchidi
your html have 2 input with name="query" ?
Val
wait lemme check
Val
yes i found its in html form
Val
will writing a different code in the index.htlm resolve the problem
inchidi
will writing a different code in the index.htlm resolve the problem
no need, you can try this def index(request): if request.method == 'GET': query = request.GET.get('query') return render(request, 'application/index.html',{'data':query}) return render(request, 'application/index.html')
Val
okay that works like magic
inchidi
already add {{ query }} at your html?
inchidi
so this code gets whatever data the user inputs
yeah, the query = request.GET.get('query') getting the input value with name query and store it to query variable
inchidi
and then you render the template with dictionary {'data':query}
inchidi
no
try it, under your form. will it raise error?
Val
try it, under your form. will it raise error?
yes it did so what should i do?
inchidi
KeyError?
inchidi
then you must check the query value {% if query %} {{ query }} {% endif %}
Val
yes
inchidi
yes
your input shown under your form after submit?
Val
when i put an input in the search and hit submit i get my url change: http://127.0.0.1:8000/application/?query=men&search=1
Val
i entered men as an input
Val
so i believe it means its working
inchidi
i should add this to my index.html
if you want to show "men" on the page
Val
okay
Val
how should i add that?
inchidi
no, put the endif right after {{ query }} else your form gonna be gone if query value is none
inchidi
if you already know how to show passed value from views then your next task is show value from this url https://api.github.com/search/repositories?per_page=5&sort=created&q=men
inchidi
but the q param value is user input (query) and not men
Val
{% if query %} {{ query }} {% endif %} for the above task?
inchidi
and then? already try running it? you see your input?
Val
no differences though still the same changes in the url
Val
no not at all
inchidi
ah i see
inchidi
change query with data. i think you are using data as key in your views.py right?
Val
lemme confirm that
inchidi
thats the problem. you can change the key to query or stay with data as key then change 'query' at template
Val
okay lemme try that
Val
it work
Val
Val
so happy
inchidi
nice, next task gonna be a bit harder
Val
well i can't go back now
inchidi
okay, so the next is you need to parse the JSON data from https://api.github.com/search/repositories?per_page=5&sort=created&q=men and send it as response to user
inchidi
you are using py2/py3?
Val
py2