Reputation: 1281
I am newbie to dJango and javascript.
I am trying to pass the variable into the parameter in my function so that the function is called using the variable.
Below are codes.
view.py
def plot_graph(request,column):
data = Data.objects.all() \
.extra(select={'data': connections[Data.objects.db].ops.date_trunc_sql('data', column)}) \
.values(column)
return JsonResponse(list(data),safe=False)
urls.py
urlpatterns = [
url(r'^form/$', views.Form),
url(r'^api/plot_graph/(?P<column>[\w-]+)', views.plot_graph, name='plot_graph'),
]
form.html
var c = {{column_json | safe}}
d3.json("{% url "plot_graph" column=c %}", function(error, data) {
data.forEach(function(d){
d.c5 = d.c5;
});
What I want to do is that variable c
is passed into the d3.json
so that function plot_graph
is used according to the variable c
.
However, below code gives me an error saying that
Reverse for 'plot_graph' with keyword arguments '{'column': ''}' not found. 1 pattern(s) tried: ['index/api/plot_graph/(?P<column>[\\w-]+)']
How to resolve this issue?
Upvotes: 0
Views: 545
Reputation: 14360
The problem here is that c
is a js variable, the context variable c
does not exist, and the url
tag is a django template tag, so it expects context variables o base types like str
, int
, ect ..
Try:
d3.json("{% url "plot_graph" column=column_json|safe %}", function(error, data) {
data.forEach(function(d){
d.c5 = d.c5;
});
You can use context variables and filters inside other tags.
On the other hand ...
You have to be sure you have the content you expect in column_json
or if after apply the safe
filter there is some useful content.
Upvotes: 1