On 14 août, 20:11, Schmidtchen Schleicher <spiolli...@googlemail.com>
wrote:
> Hey Guys,
> my problem is, as shown in the subject, that the template-engine puts in the
> name of the templatevariable instead of the value:
>
> my view:
>
> def YearlyView(request, calid, year):

The convention is to use all_lower names for functions.

>     lst = []

This one isn't used anywhere

>     k = get_object_or_404(Kalender, pk=calid)
>     now_year, now_month = datetime.date.today().year,
> datetime.date.today().month

You make one useless call to datedatime.date.today here.

>     mlst = []

Readable names help.

>     for n, month in enumerate(mnames):

where does this mnames comes from ?

>         entry = current = False
>         entries = k.termin_set.filter(date__year=year, date__month=n+1)
>
>         if entries:
>             entry = True

If all you want to know is if there are entries for this year and
month, you could just use Queryset.count(). And directly evaluating it
as a boolean expression to avoid the useless if.

>         if year == now_year and n+1 == now_month:
>             current = True

Idem... Why write "if some_boolean_expression: x = True" when you can
write : "x = some_boolean_expression" ?

>         mlst.append({'n':n+1, 'month':month, 'entry':entry,
> 'current':current})

Clean code formatting helps too

>     return render_to_response("kalender/year.html", {'months':mlst,
> 'user':request.user,

You wouldn't need to pass request.user if you used a RequestContext
and the appropriate context processor.


def yearly_view(request, calid, year):
    k = get_object_or_404(Kalender, pk=calid)

    # local alias, will save a lot of computing...
    termin_set =  k.termin_set.filter

    today = datetime.date.today()
    now_year, now_month = today.year, today.month

    month_list = []
    for month_num, month_name in enumerate(month_names):
        month_num +=1
        has_entry = termin_set(
            date__year=year,
            date__month=month_num
           ).count() > 0
        is_current = year == now_year and month_num == now_month
        month_list.append({
             'month_num':month_num,
             'month_name':month_name,
             'has_entry': has_entry,
             'is_current':is_current
             })

    context = {
         'month_list':month_list,
         'user':request.user,
         'year':year,
         'calid':calid
        }

    return render_to_response(
        "kalender/year.html",
        context
        )


> Now i want to iterate through mlst alias 'months' in the template:
>
> {% for current, entry, month, n in months %}

Please think about what "months" is here... Yes, a list of dicts. So
this line is the equivalent of:

current = months[0] # first dict in the list
entry = months[1]    # second dict in the list
month = months[2]  # etc....
n = months[3]


> And another question: In the view the order of the key-value-pairs in the
> dict mlst

It's *not* a dict. It's a list of dicts

> is set as 'n, month, entry, current' but in the template it is
> reversed...
> Why?


Python dicts are unordered.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to