python - writing urlpattern in django for rendering an image -


in django app,i have index page lists summary info dynamic(based on user input data in db) . have coded below

views.py

def custom_render(request,context,template):     req_context=requestcontext(request,context)     return render_to_response(template,req_context)  @login_required def index(request, template_name):     summary_info = get_summary(...)     return custom_render(request,{'summary':summary_info},template_name) 

urls.py

urlpatterns=patterns('',       ...     url(r'^$', 'myapp.views.index',dict(template_name = 'myapp/index.html'), name = 'home'), ... 

now,i want include chart image generated matplotlib on home page..so,when user requests index page url, can see both summary info , chart

i have written index.html below

{% extends "myapp/base.html" %} .... <div id='summary'> {# here display summary #} ... </div>  <div id='chart'> <img class="chartimage"src="{% url myapp_render_chart %}"                 alt="chart"                /> </div> 

the chart view

def render_chart(request):     data = get_data(request.user)     canvas = none     if data:         canvas = create_piechart(data)     response = httpresponse(content_type = 'image/png')     if canvas:         canvas.print_png(response)     return response  import matplotlib.pyplot plt def create_piechart(data,chartsize=(16,16)):     ...     figure = plt.figure(figsize = chartsize)     plt.pie(fracs, labels=labels, autopct='%1.1f%%', shadow=true)             canvas = figurecanvas(figure)     plt.close(figure)     return canvas 

i not sure how should urlmapping.the url r'^$', mapped index page.but need create url(...) in urlpatterns sothat view render_chart() associated name myapp_render_chart , can called within {% url %} tag . can pls me this?

so want url mapping? wouldn't different 1 have. e.g.:

urlpatterns = patterns("myapp.views",     url(r'^$', 'index',dict(template_name = 'myapp/index.html'), name = 'home'),     url(r'^kick-ass-chart/$', 'render_chart', name='myapp_render_chart'), ) 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -