Using Javascript and AJAX in the Fetch()API method: returns no values as a response

Why?

The data you get back from the view to be displayed in the table has nothing to do with anything else. It especially has nothing to do directly with any models in Django. At this point (after being returned from the view) that data is just that - data. Changing the name doesn’t facilitate anything.

I don’t mean to say output again, but at this point we have explored a lot of aspects to displaying data in ‘Table3’. I’m sending you another screenshot to remind you of what happened with getting an actual response, but with no values corresponding to the ‘Assignments’ field in ‘Table1’:

Hopefully, you get me when I am trying to say that there is a response here in a cell of ‘Table3’, however the data is incorrect.

Please show the response you’re getting from the server from the browser’s developer tools. (You can either look at the response from the network tab, or you can add a console.log of the data in your fetch call.)
We’re still not at the point of trying to do anything with this data within the HTML. We still need to verify that you’re getting the right data returned.

OK. I understand. Thanks again

I will have another sleep over this and keep reading up. I’ll be in contact with you again early tomorrow.
Thanks so much for your help thus far.
Andrew

OH. I didn’t see your post here. I will send this to you today, just give me a few minutes.
Thanks

Ok - I know you’re probably off-line by now, but before I forget - check your server console log to verify that you can see the request being made, and check for any errors from the response.

Hi. Yes I was off-line… back at it now though.
I am not that skilled in using the terminal to check the server console and so I am using Api Dog - to place the url and determine what the JSON Response is. When using this App an exception related to the change handler was raised, which seems to be the reason why I cannot get a response from the url used in my FetchAPI method.
I am attaching a screenshot below:

This also brings up one of the questions you were trying to answer earlier I think. Does the URL path related to this view make sense and is it correct ?

There are also different points that I have been trying to address relating to the parameters that are associated with this URL.
I think that lastAssignmentSelected is a parameter as I see on Aping and this makes sense because it is used in my data variable in the ‘getfirstAssignmentSeleted’ view as well as well as ini the JS function used for the Fetch API. So… I hear you when you say, not everything is related, but I am concerned that I am referring to parameters that are not properly connected / parsed / linked by a proper path / URL.

I know we are working on getting the data correct. I just think this is important because I have been reading up a bit again on JSON responses and the like in Django. I think I totally agreed with you when you said I am getting confused about what the Django server is capable of and what its role is in this situation. I think my understanding of how this procedure involved the server is improving.
Thanks

when I follow the link to the URL used in the fetch() method - the following error is returned in the Django terminal:

[28/Aug/2024 08:53:19] "GET /getfirstAssignmentSelected/ HTTP/1.1" 404 3594

OK. in my schedule_project urls.py file there was a spelling mistake in url patterns, for the path ‘getfirstAssignmentSelected’. Now I can see that there is a Field Error and it makes sense because it is what I was trying to say.
I am posting this error to you below:


FieldError at /getfirstAssignmentSelected/<lastAssignmentSelected>
Cannot resolve keyword 'Assignments' into field. Choices are: amc_099, assignment, clinical_admin_stocktake, colpo_LEEP, id, ighid_12022, ighid_12046, patient_scheduling, screening_results_reports, sll_qHPV
Request Method:	GET
Request URL:	http://localhost:8000/getfirstAssignmentSelected/%3ClastAssignmentSelected%3E
Django Version:	5.0.6
Exception Type:	FieldError
Exception Value:	
Cannot resolve keyword 'Assignments' into field. Choices are: amc_099, assignment, clinical_admin_stocktake, colpo_LEEP, id, ighid_12022, ighid_12046, patient_scheduling, screening_results_reports, sll_qHPV
Exception Location:	/Users/andrewchibwesha/Visual Studio Projects/test_app/.venv/lib/python3.12/site-packages/django/db/models/sql/query.py, line 1772, in names_to_path
Raised during:	schedule_app.views.getfirstAssignmentSelected
Python Executable:	/Users/andrewchibwesha/Visual Studio Projects/test_app/.venv/bin/python3
Python Version:	3.12.4
Python Path:	
['/Users/andrewchibwesha/Visual Studio Projects/test_app/schedule_project',
 '/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python312.zip',
 '/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12',
 '/usr/local/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/lib/python3.12/lib-dynload',
 '/Users/andrewchibwesha/Visual Studio '
 'Projects/test_app/.venv/lib/python3.12/site-packages']
Server time:	Wed, 28 Aug 2024 09:10:18 +0000

That’s going to be required here.

Somewhere, somehow, you’re running runserver (or one of its alternatives). The output produced by it is frequently necessary for debugging.

What you see from the client side is only half the picture.

Let’s clarify something else here:

This url definition:

Does not mean that you issue a request to /getfirstAssignemntSelected/<lastAssignmentSelected>

The <lastAssignmentSelected> is a placeholder for a parameter to be supplied. In your JavaScript, it’s written as:

Where that parameter is a reference to a variable in your JavaScript code by that name. (Note that the name here does not have any relationship to the name used by the url definition. This name applies to your JavaScript, the other applies to Django. The only information being passed is the value of that variable in the URL.

Review the docs at URL dispatcher | Django documentation | Django for more details about this.

Thanks. I see. I think the statement I am using refers to ‘the route argument’ there.
I am going to attempt to read up on working in the Terminal as well.

Is the MultiValueDict Key Error occurring because the data that I have puts the field names into a dictionary without any of that data being saved? However, when trying to receive or get the response - Assignments.objects.all() prints the field name but then None. As to say it is still in the format that it was modeled / coded in, in the app, but not saved into the database ?
I am reading up on the Django Shell.

This could indicate that {{ form.data }} is the right selector to use because of how the API is setup. And also indicate that the variable data points to the correct model and associated fields, but something happens after that, like the fields are read as None because they are not saved to the database correctly ? Am I on the right path ?

Another thing about my data when I try to access it in the shell is that the type object ‘Assignments’ has no attributes.
Do I follow something like this below to ensure it does ?:

class Dog:
    def __init__(self, name):
        self.name = name

my_dog = Dog(&quot;Buddy&quot;)
print(my_dog.name)

This result seems to say that Assignments is a class:

>>> print(Assignments)
<class 'schedule_app.models.Assignments'>

However, I’d like to print and access the contents of that class / Model. Which is not happening and If I think about the schedule_app - I think this is one part of the data that is missing as well.