如何在dJango网址中指定字典参数?

时间:2019-01-17 07:00:57

标签: django

我是Django的新手,并收到以下错误消息

Reverse for 'plot_graph' with keyword arguments '{'column': '["c4", "c5"]'}' not found. 2 pattern(s) tried: ['index/api/plot_graph/(?P<column>[0-9]+)$', 'index/api/plot_graph/(?P<column>[\\w-]+)/$']

我的网页显示了五个不同的复选框,分别为c1,c2,c3,c4,c5,当我单击c4,c5然后单击“提交”按钮时,我可以看到上述错误。

以下是我的文件。

urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^form/$', views.Form),
    # url(r'^api/plot_graph/', views.plot_graph, name='plot_graph'),
    url(r'^api/plot_graph/(?P<column>[\w-]+)/$', views.plot_graph, name='plot_graph'),
    url(r'^api/plot_graph/(?P<column>[0-9]+)$', views.plot_graph, name='plot_graph'),

views.py

def plot_graph(request,column):
    print(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)


def Form(request):
    if request.method == 'POST':
        if len(request.FILES) !=0:
            file = request.FILES['files']
            column_name = upload_file_name(file)
            return render(request,"index/form.html",{"column_name":column_name})
        else:
            columns = request.POST.getlist('columns')
            column_json = json.dumps(columns)
            return render(request,"index/form.html",{"column_json":column_json})
    else:
        return render(request,"index/form.html",{})

form.html

d3.json("{% url "plot_graph" column=column_json|safe %}", function(error, data) {
  data.forEach(function(d){
    d.c5 = d.c5;
});

就我而言,如何更改网址以使其起作用?

0 个答案:

没有答案