开发者

how to pass a year value to a django view through a link in html

开发者 https://www.devze.com 2023-03-20 14:30 出处:网络
My django app has a couple of methods to draw a chart from given data. I have put alink for producing chart display page as below

My django app has a couple of methods to draw a chart from given data. I have put a link for producing chart display page as below

<ul id ="dataplots" class="report">
    <li id="alldataplots_list" class="report">
        <a href="{% url alldata_report   %}"> All data Plot </a>
    </li>
    ...
</ul>

Similarly ,I want to give links to create plot of data for a particular year

I have coded the urls.py and views.py as below.What I could not figure out was,how to provide the link which would invoke

http://127.0.0.1:8000/myapp/data/2011

sothat the view

'create_report_for_data_of_the_year' 

is run.I can type this in browser bar and run it. But I want to provide it as a link.Is a drop down list for the years the answer? Will I have to provide a form, submit button etc and send the year as post data?

Any help/advice most welcome

thanks

mark

urls.py

urlpatterns=patterns('',

url(r'^data/(?P<year>\d{4})/$','myapp.views.create_report_for_data_of_the_year',
    {
        'template_name':'myapp/report_for_data_of_the_year.html',
        'page_title':'report for data in the Year'

    },name='report_data_for_year' ),

url(r'^data/$','myapp.views.create_report_for_alldata',
    {
        'template_name':'myapp/alldata_report.html',
        'page_title':'All data report'

    },
    name='alldata_report'),

)

views.py

def create_report_for_alldata(request,page_title,template_name):
    dataset=MyDataModel.objects.filter(creator=request.user)
    map = create_map_of_names_and_values(dataset)
    basefilename = "alldataplot"
    imgfilename = create_plot(map,basefilename)
    report_data={'basefilename':basefilename,'plot_image':imgfilename,'page_title':page_title}
    report_data["name_value_dict"]=map
    req_context=RequestContext(request,context)
    return render_to_response(template_name,req_context)

def create_report_for_data_of_the_year(request,year,page_title,template_name):
    dataset=MyDataModel.objects.filter(today__year=year,creator=request.user)
    #today is a field in MyDataModel and is a datetime.datetime 
    map = create_map_of_names_and_values(dataset)
    basefilename = "plotofdataforyear%s"%year
    page_title = page_title+" "+yea开发者_JAVA百科r
    imgfilename= create_plot(map,basefilename)
    report_data={'basefilename':basefilename,'plot_image':imgfilename,'year':year,'page_title':page_title}
    report_data["name_value_dict"]=map
    req_context=RequestContext(request,context)
    return render_to_response(template_name,req_context)
...


No, you don't have to use a post. This example uses an inline click handler, which you might want to refactor using jQuery or some other library, but it shows how to use a drop-down to navigate. http://lab.artlung.com/dropdown/

0

精彩评论

暂无评论...
验证码 换一张
取 消