Django crispy form is not posting or view is incorrect
I am currently working on a note/comment system. It is intended to work as adding notes to each individual projects. For example, you set up a "Project" and there is a note or comment sections intended for adding updates to the project. Here is my view.py for updating the project. The top portion is for updating the project itself, the bottom portion is for adding the notes.
@login_required(login_url="/login")
def update_project(request, pk):
queryset = DevProjects.objects.get(id=pk)
form = UpdateProject(instance=queryset)
if request.method == 'POST':
form = UpdateProject(request.POST, instance=queryset)
if form.is_valid():
form.save()
return redirect('/')
project = get_object_or_404(DevProjects, id=pk)
notes = project.notes.filter(id=pk)
new_note = None
if request.method == 'POST':
notes_form = AddProjectNotes(request.POST, instance=project)
if notes_form.is_valid():
new_note = notes_form.save(commit=False)
new_note.project = project
new_note.save()
else:
notes_form = AddProjectNotes()
return render(request, 'projects/updateprojects.html', {"form": form, "queryset": queryset, 'project': project, 'notes': notes, 'new_note': new_note, 'notes_form': notes_form})
Whenever I try and submit the notes, it shows that is successfully posted to DB but in DB there is no entries. Here is my template.
<div class="container">
{% if new_note %}
<h2>Your comment has been added.</h2>
{% else %}
<h2>Add a new comment</h2>
<form action="." method="post">
{{ notes_form.notes|as_crispy_field }}
{% csrf_token %}
<p><input type="submit" value="Add comment"></p>
</form>
{% endif %}
</div>
<div class="container">
{% for note in notes %}
<div class="notes">
<p class="info">
Comment {{ forloop.counter }} by {{ note.notes }}
{{ note.created_at }}
</p>
</div>
{% empty %}
<p>There are no notes yet.</p>
{% endfor %}
</div>
It is supposed to work as a commenting system, so I make the post related to each specific project. Here are my models:
class DevProjects(models.Model):
project_title = models.CharField(max_length=100)
project_desc = models.CharField(max_length=500)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateField(auto_now=True)
created_by = models.ForeignKey(User, on_delete=models.CASCADE)
class DevProjectNotes(models.Model):
project = models.ForeignKey(DevProjects, on_delete=models.CASCADE, related_name='notes')
notes = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
added_by = models.ForeignKey(User, on_delete=models.CASCADE)
What am I doing wrong to where the data will not post to the database? I need it to be related to the project as it is intended for notes to be added related to the project. Any and all help is greatly appreciated.
I have tried playing around with the view. I can make the posts via Django admin but I cannot post via template. Even though I get the POST 302 0 code.