i am learning Django tutorial, could you pls advise me how to edit cod to get search result to put into database? Below are my cod and sure that the rest of cod in this tutorial i did correctly and test complete, many thanks
I have updated the code as per your request. The result on home page fail is also attached at the bottom of the question section. If you have any additional question, please ask and I will be able to share.
def __str__(self):
"""Returns a string representation of a message."""
date = timezone.localtime(self.log_date)
return f"'{self.message}' logged on {date.strftime('%A, %d %B, %Y at %X')}"
class HomeListView(ListView):
"""Renders the home page, with a list of all messages."""
model = LogMessage
def get_context_data(self, **kwargs):
context = super(HomeListView, self).get_context_data(**kwargs)
return context
def log_message(request):
form = LogMessageForm(request.POST or None)
if request.method == "POST":
if form.is_valid():
query = form.save(commit=False)
for j in search(query, tbs='qdr:h', num=10, start=0, stop=10, pause=2.0):
message = LogMessage(log_date = datetime.now(), message =j)
message.save()
return redirect("home")
else:
return render(request, "hello/log_message.html", {"form": form})
--- HOME PAGE FAIL INFO
TypeError at /log/
quote_from_bytes() expected bytes
Request Method: POST
Request URL: http://127.0.0.1:8000/log/
Django Version: 3.1.5
Exception Type: TypeError
Exception Value:
quote_from_bytes() expected bytes
Exception Location: C:UsersAdministratorAppDataLocalProgramsPythonPython38-32liburllibparse.py, line 866, in quote_from_bytes
Python Executable: C:UsersAdministratorAppDataLocalProgramsPythonPython38-32python.exe
Python Version: 3.8.7
Python Path:
['D:\Apps\07.1\hello_django',
'C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\python38.zip',
'C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\DLLs',
'C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib',
'C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32',
'C:\Users\Administrator\AppData\Roaming\Python\Python38\site-packages',
'C:\Users\Administrator\AppData\Local\Programs\Python\Python38-32\lib\site-packages']
Server time: Thu, 07 Jan 2021 14:38:11 +0000
Traceback Switch to copy-and-paste view
C:UsersAdministratorAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersexception.py, line 47, in inner
response = get_response(request) …
? Local vars
C:UsersAdministratorAppDataLocalProgramsPythonPython38-32libsite-packagesdjangocorehandlersase.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
? Local vars
D:Apps7.1hello_djangohelloviews.py, line 34, in log_message
for j in search(query, tbs='qdr:h', num=10, start=0, stop=10, pause=2.0): …
▼ Local vars
Variable Value
form
<LogMessageForm bound=True, valid=True, fields=(message)>
query
<LogMessage: '333321212121121221' logged on Thursday, 07 January, 2021 at 14:38:11>
request
<WSGIRequest: POST '/log/'>