I have been struggling with one of my pages in django. Basically I a making a graph (nodes and edges) where you can add remove nodes through forms. I have a graph model (GraphDetail) which requires nodes, and I am building the node form (GraphCreateNode) that adds nodes to the graph. However the link does not work. It says that GraphCreateNode does not take any arguments. Now CreateGraphNode needs the id of the graph it is going to be added to, whether that is sent via the url or stored and accessed in session I do not have a preference. Right now I am trying via the url. And as said I get an error, and I cannot see where I need to fix it, which is why I am asking all of you.
Error Message:
TypeError at /graphs/graph/74921f18-ed5f-4759-9f0c-699a51af4307/graph_add_node/
GraphCreateNode() takes no arguments
So I looked around here to see what I could find: What is a NoReverseMatch error, and how do I fix it? was quite informative but not quite enough. Then I found TypeError: CraiglistScraper() takes no arguments, whilst it was about Selinum it mentioned something about a constructor. My class does not have a constructur, but does it need one? or is the problem a different one.
Here are the two views. GraphDetail and the GraphCreateNode. I am on the GraphDetail page and click the link to go to GraphCreateNode
GraphDetail:
class GraphDetailView(generic.DetailView):
model = Graph
slug_field = '<uuid:id>'
slug_url_kwarg = '<uuid:id>'
GraphCreateNode:
class GraphCreateNode:
def add_a_node(request):
new_node = get_object_or_404(Node, pk)
if request.method == "POST":
form = AddNodeForm(request.post)
if form.is_valid():
new_node.name = form.cleaned_data['name']
new_node.summary = form.cleaned_data['summary']
new_node.description = form.cleaned_data['description']
new_node.due_date = form.cleaned_data['due_date']
new_node.completed = form.cleaned_data['completed']
new_node.neighbor_nodes = form.cleaned_data['neighbor_nodes']
new_node.tags = form.cleaned_data['tags']
new_node.save()
graph = Graph.objects.get(pk=pk)
graph.nodes.add(new_node)
graph.save()
return HttpResponseRedirect(reverse('graph-detail') )
else:
form = AddNodeForm()
graph = Graph.objects.get(pk=pk)
context = {
'form': form,
'graph': graph,
}
return render(request, 'graphs/graph_add_node.html')
Here are the two html pages
GraphDetail which works so I have only included the part where I then click to get to GraphCreateNode
{% else %}
<p>There are no nodes assigned to this Graph.</p>
{% endif %}
<p><a href="{% url 'graph-create-node' graph.pk %}">Add Node</a></p>
<hr>
The GraphCreateNode html code:
{% extends "base_generic.html" %}
<h1>Adding a node to graph: {{ graph.name }} </h1>
<form action="" method="post">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type="submit" value="Submit">
</form>
{% block content %}
{% endblock %}
Here are the urls.
path('graph/<uuid:pk>', views.GraphDetailView.as_view(), name='graph-detail'),
path('graph/<uuid:pk>/graph_add_node/', views.GraphCreateNode, name= 'graph-create-node'),
Here is the modelfrom for the node form.
from django.forms import ModelForm
from .models import Node
class AddNodeForm(ModelForm):
class Meta:
model = Node
fields = ['name', 'summary', 'description', 'due_date', 'completed', 'neighbor_nodes', 'tags']
Im still new to Django and am having a hard time understanding how I would send the graph id from the first page graphdetails too GraphCreateNode. Let me know if I need to add more info or clarify things.