简体   繁体   中英

Passing multiple variables from the template to a view

I am trying to pass a few variables to my view in django. The information needed is whether the user wanted the next or previous month, and the current month. Here is what I have got so far...

<a href="{% url 'tande:holiday' %}?value=previous" class='previous' id='previous' name='previous'><<-Previous</a>&nbsp;&nbsp;<a href="{% url 'tande:holiday' %}?value=next" name = 'next' class='next' id='next'>Next->></a>

I've got the next and previous bits to work - no problem! But I can't get the year and month information. The view:

def holiday(request, value=None):
    if request.method == "GET":
        if value != None:

        #THIS IS WHAT I NEED TO GET
        current_year = request.GET.get('year')
        current_month = request.GET.get('month')

        value = request.GET.get('value')

        if value == "next":
            #calculate next month
            year = next_month.year
            month = next_month.month
        if value == "previous":
            #calculate previous month
            year = last_month.year
            month = last_month.month
    else:
        date_today = datetime.now()
        year = date_today.year
        month = date_today.month

#......

# render html calendar with a form for input requesting holiday
context = {

    # dont know if i need this...??

    "year": year,
    "month": month,
}
return render(request, "tande/calendar.html", context)
  • Can I pass multiple variables in my url? ie <a href="{% url 'tande:holiday' %}?value=previous?year={{ year }}?month={{ month }}" ...

Thanks

You need to use & to separate GET parameters.

<a href="{% url 'tande:holiday' %}?value=previous&year={{ year }}&month={{ month }}"

I don't understand your second question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM