开发者

using get or post to pass argument to django view

开发者 https://www.devze.com 2023-03-20 02:24 出处:网络
In my django app ,I need to call a view with an argument named \'year\'. Then in the template,I created a form and a dropdown list using a list of year names,At this point,I am confused as to how the

In my django app ,I need to call a view with an argument named 'year'. Then in the template,I created a form and a dropdown list using a list of year names,At this point,I am confused as to how the view should be invoked.

The view is named 'create_report_for_data_of_the_year'.It expects a year argument. ie,

http://127.0.0.1:8000/myapp/reports/2011

I tried to write the template as shown below.

<li id="yearlydataplots" class="report">
    <form action="create_report_for_data_of_the_year" >
        <select name="year" id="year">
            {% for anyear in years %}
                <option  value={{anyear}} > {{anyear}}</option>
            {% endfor %}
        </select>

        <input type="submit" value="Plot for entries of the year"/>
       </form>
    </li>

However,when the submit button is clicked,the browser goes to

http://127.0.0.1:8000/myapp/reports/create_report_for_data_of_the_year?year=2006

which causes a 404.

I changed method="post", and clicking submit goes to

http://127.0.0.1:8000/myapp/reports/create_report_for_data_of_the_开发者_Go百科year

which again causes 404

I know,I am missing something very basic :-)..If someone can kindly point it out,it would be nice

thanks in advance,

mark

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+" "+year
    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)

and url mapping is

...
url(r'^reports/(?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' ),
...


You need to do a redirect to desired page with JS. Or you can accept a year as a get paramener with year = request.GET.get('year').

0

精彩评论

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