'Why am I only getting default values when adding something to my database? (Django)
I'm trying to add tasks to my todo list. All of my added tasks are listed on the homepage (home.html) but whenever I try to add something it always sets my input to default (I added a picture at the end). I know I wrote 'default_value' in views but I wanted to check if it's even adding todos without it throwing an error.
views.py
def newTodo(request):
obj = List()
obj.taskText = request.POST.get("taskText", "default_value")
obj.taskDate = request.POST.get("taskDate", "default_value")
obj.taskDerc = request.POST.get("taskDerc", default= 5)
obj.save()
mydict = {
"all_tasks" : List.objects.all()
}
return render(request, 'newTodo.html', context=mydict)
newTodo.html (using Bootstrap)
<tbody>
{% block content %}
<tr>
<form action="{% url 'newTodo' %}">
<td scope="row">
<input type="text" class="form-control" id="taskText" name="taskText"></td>
<td scope="row">
<input type="text" class="form-control" id="taskDate" name="taskDate"></td>
<td scope="row">
<input type="text" class="form-control" id="taskDerc" name="taskDerc"></td>
<td><a href="{% url 'home' %}" class="btn btn-success" role="button">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-check" viewBox="0 0 16 16">
<path d="M10.97 4.97a.75.75 0 0 1 1.07 1.05l-3.99 4.99a.75.75 0 0 1-1.08.02L4.324 8.384a.75.75 0 1 1 1.06-1.06l2.094 2.093 3.473-4.425a.267.267 0 0 1 .02-.022z" />
</svg>
</a>
</td>
</form>
</tr>
{% endblock %}
</tbody>
What am I missing? Any kind of help is appreciated!
EDIT/UPDATE I fixed it myself. apparently, you can't use this table from bootstrap I used in newTodo.html, at least not in this form. It couldn't find any inputs so it always used the default_value. I also changed
<form action="{% url 'newTodo' %}">
to
<form action="{% url 'submit' %}" method="get">
Because whenever I clicked on newTodo.html it automatically generated a new todo instead of only doing so when adding inputs. I changed "newTodo" in views.py to "submit" and some other stuff, but yeah, that's it in general.
Solution 1:[1]
I may be wrong but try to replace those lines of code in your file newTodo.html :
<a href="{% url 'home' %}" class="btn btn-success" role="button"><!–– your SVG ––></a>
by something like
<button type="submit" class="btn btn-success"><!–– your SVG ––></button>
I think you currently have no way to submit the data in your form, you need a button of type "submit" for the data to be sent to the URL of the form (see here for more details).
This does not directly answer your question but by the way, here are some tips to improve your code:
- Don't name your model
List, it could lead to confusion with Python'slist()built-in which returns alist, - Check that the request is a POST request before trying to access
request.POST, or you could get a500 internal server error. Use the decoratorrequire_POST()for instance (cf. documentation), - Add a CSRF token
{% csrf_token %}to your HTML form and make it mandatory in the POST request, for instance by adding the decorator@requires_csrf_tokento your view (check this page for more information about CSRF in Django), - Finally, as suggested in the comment of @Erik Kalkoken, you should use Django's form API as much as possible and avoid building your forms from scratch. It will save you hours of work and it will be much cleaner.
Please don't forget to accept this answer if it helped.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sc?riolus |

