【问题标题】:Link is not working in django链接在 django 中不起作用
【发布时间】:2013-07-01 12:58:11
【问题描述】:
<a href = "{% url 'ngraph' %}">Customer Count</a>

当我点击客户计数时,它只工作一次,为了让它工作,我必须再次运行程序,这里有什么问题?我注意到的是,一旦单击链接 localhost:8000/graph (客户计数),它就可以工作,但会使 localhost:8000 忙,并且该链接不起作用。如何解决?

views.py

def graph(request):
    age = ['below 20', '20-22','22-24','24-26','26-28','28-30','30-40','above 40']
    avg_call_group =  [0, 0, 0, 0, 0, 0, 0, 0]

    cursor = connection.cursor()
    cursor.execute("select p.age_group,sum(c.avg_duration) as age_avg_dur from demo p,(select  card_no as card_n, avg(duration) as avg_duration from call_details where service_key = 1 and card_no =calling_no group by card_no) as c where p.card_no = c.card_n group by p.age_group ")
    numrows = int(cursor.rowcount)
    for x in range(numrows):
        row = cursor.fetchone()
        avg_call_group[x] =  row[1]

    cursor.close()

    import matplotlib.pyplot as plt
    f=plt.figure(figsize = (3,3))
    exploding = [.04, .04, .04, .04 , .04, .04, .04, .04]
    age = ['below 20', '20-22','22-24','24-26','26-28','28-30','30-40','40-50']
    plt.pie(avg_call_group, labels= age, autopct='%1.3f%%', explode = exploding)
    canvas = FigureCanvasAgg(f)
    response = HttpResponse(content_type='image/png')
    canvas.print_png(response)
    return response

urls.py

from django.conf.urls import patterns, include, url
from welcome.views import *
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^admin/', include(admin.site.urls)),
    url(r'^hello/', hello, name = 'nhello'),
    url(r'^graph/', graph, name = 'ngraph'),
)

home.html

<div >
<a href = "{% url 'ngraph' %}">Customer Count</a>
</div>

替代源代码位置: click here

【问题讨论】:

  • 您是否禁用了浏览器缓存? stackoverflow.com/questions/2095520/…
  • 这对测试也一样吗?
  • 可能是链接调用的视图抛出异常并且您的服务器死机,您可以访问服务器日志/输出吗?
  • 您没有在上下文中发回响应
  • @DRC 是的,我已经访问了服务器日志/输出但不知道?

标签: python html django


【解决方案1】:

我不知道你是否还需要答案,但我解决了这样的确切问题:

matplotlib.use('Agg') # After you import


# And these lines at the end

fig.clf()
plt.close()
plt.clf()
del var_grid
gc.collect()

【讨论】:

    【解决方案2】:

    在返回您的视图代码时,执行重定向。类似的东西:-

    return render_to_response('graph.html', context_instance=RequestContext(request))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-11
      • 2017-08-31
      • 1970-01-01
      • 2020-09-20
      • 2014-04-12
      • 1970-01-01
      相关资源
      最近更新 更多