Open KagemaNjoroge opened 2 weeks ago
Hey! Correct me if I am wrong, but I think that in regards to the
- JSON Parsing Error:
the problem is that you are sending a string instead of the proper JSON Response. If your model is
class Expense(models.Model):
expense_name = models.CharField( max_length=100, blank=False, null=False, unique=True )
expense_description = models.CharField(max_length=300, blank=True, null=True)
category = models.ForeignKey(ExpenseCategory, on_delete=models.CASCADE)
amount = models.FloatField(default=0.00)
date_added = models.DateTimeField(auto_now_add=True)
date_modified = models.DateTimeField(auto_now=True)
then you should pass the data in the form of a JSON, and each key should correspond to a field of the model.
For example, an appropriate input could be:
{"expense_description": "your description",
"category": "your FK",
"amount": "your amount",
"date_added": "your_date",
"date_modified": "your_date"}
I don't really know which kind of data you are passing to the api from javascript, but if it is something like this:
django.urls.exceptions.NoReverseMatch: Reverse for 'index' not found. 'index' is not a valid view function or pattern name.
as you stated before it will throw an error.
Hope my answer was what you were looking for and tell me if you need help with something else :)
edit: i embeded the code correctly
Thanks very much for the help. Could you help me migrate the views in expenses
app to standalone rest_framework views.
@darthpedroo
Body:
I'm currently migrating my Django project from plain Django views to Django REST framework views. Specifically, I'm working on
expenses/views.py
andtemplates/expenses/index.html
. However, I'm facing several issues during this migration.Issue Details:
1. Template Rendering Error:
I'm encountering an error when trying to render the
index.html
template for the GET request. The specific error is:2. URL Reverse Error:
When trying to reverse the URL for the 'index' view, I get the following error:
3. JSON Parsing Error:
When making a POST request, the server returns a 400 Bad Request error with the message:
This is the data being sent:
Code Details:
expenses/views.py
:JavaScript Code:
Additional Information:
Any help or guidance to resolve these issues would be greatly appreciated. Thank you!