从模板中的模型中获取函数

时间:2016-11-15 09:23:42

标签: django django-models django-templates

我正在创建一个简历应用程序,不知道我怎么能得到 我模板中模型的get_relative_length函数, 数据没有通过,我需要帮助,我是django的新手 我非常感谢你的帮助

模型

from __future__ import unicode_literals

#from django.db import models

# Create your models here.
from datetime import datetime
from dateutil import relativedelta

from django.db import models
from django.utils.translation import ugettext_lazy as _
#from djangocms_text_ckeditor.fields import HTMLField


class CreateCV(models.Model):
    title = models.CharField(verbose_name=_('Title'), max_length=255)
    company = models.CharField(verbose_name=_('Company'), max_length=255)
    start_date = models.DateField(verbose_name=_('Start date'), help_text=_(
        "The date when you started this position - only the month and year will be displayed"))
    end_date = models.DateField(verbose_name=_('End date'), blank=True, null=True, help_text=_(
        "The date when this position ended - only the month and year will be displayed. You don't have to define this if it is your active post."))

    active_post = models.BooleanField(verbose_name=_("Active position?"), help_text=_(
        "Check this if this is your active post. You won't have to add the end date in that case."))

    description = models.TextField(verbose_name=_("Description"),
                                   help_text=_("Give a short description about your work and responsibilities."),
                                   max_length=2048,
                                   null=True, blank=True)

    website = models.CharField(verbose_name=_("Website"), help_text=_("Provide a link to the company's website."),
                               max_length=255, null=True, blank=True)

    show_year = models.BooleanField(verbose_name=_("Show Year"), help_text=_('Displays how long the current position was held.'), default=False)

    def __unicode__(self):
        return self.title

    def get_month_diff(self, d1, d2):
        """
        Counting up the months from d1 (start date)
        Until d2 (end date OR today) is reached.
        Args:
            d1: Start Date
            d2: End date
        Returns: Months
        """

        delta = relativedelta.relativedelta(d2, d1)
        months = (delta.years*12) + delta.months

        return months

    @property
    def get_month_diff_string(self):
        """
        Simple method to humanize the months from function
        get_month_diff
        Returns: diff_string
        """

        if self.active_post:
            d2 = datetime.now()
        else:
            d2 = self.end_date

        month_diff = int(self.get_month_diff(self.start_date, d2))
        if month_diff < 12:
            diff_string = (str(month_diff) + ' ' + str(_('Months')))
            if month_diff <= 1:
                diff_string = (str(1) + ' ' + str(_('Month')))
        else:
            if month_diff % 12 == 0:
                year_diff = str(month_diff/12)
            else:
                year_diff = str(round(float(month_diff)/12, 1))
                print(year_diff)
            diff_string = (year_diff + ' ' + str(_('Years')))
            if year_diff == '1':
                diff_string = (str(1) + ' ' + str(_('Year')))

        return diff_string

    @property
    def get_relative_length(self):
        """
        Method to get the relative length to
        the longest length.
        Everything below 18% gets up'd to 18% (design reasons)
        Returns: length_percentage
        """

        longest_post = self.get_longest_post()

        if self.active_post:
            end_date = datetime.now()
        else:
            end_date = self.end_date

        relative_percentage = (float(self.get_month_diff(self.start_date, end_date)) / float(longest_post)) * 100

        if relative_percentage <= 18:
            length_percentage = 18
        else:
            length_percentage = relative_percentage

        return int(length_percentage)

    def get_longest_post(self):
        """
        Get the post object with the longest duration
        Returns: longest (amount of months)
        """
        longest = 0
        for post in Post.objects.all():

            if post.active_post:
                d2 = datetime.now()
            else:
                d2 = post.end_date

            diff = self.get_month_diff(post.start_date, d2)

            if diff > longest:
                longest = diff

        return longest

观点

# Create your views here.
from django.shortcuts import render

# Generics views
from django.views.generic import ListView, DetailView

# Models
from .models import CreateCV


class CreateCVListView(ListView):
    model = CreateCV
    template_name = 'cv/cv_list.html'
    context_object_name = "cv_list"

模板

{% extends "base.html" %}
{% load i18n %}

{% block content %}
{% for cv in cv_list %}


<div class="position-entry"
     id="position-{{ cv.pk }}">

    <div class="position-duration {% if forloop.last %}last{% endif %} {% if forloop.first %}first{% endif %}">
        <div class="duration-circle"
             style="width: {{ instance.get_relative_length }}px;
                     height: {{ instance.get_relative_length }}px;
                     margin-left: -{% widthratio cv_list.get_relative_length 2 1 %}px;
                     margin-top: -{% widthratio cv_list.get_relative_length 2 1 %}px;
                     "></div>
        {% if cv.get_relative_length > 18 %}
            <div class="duration-label" style="{% if cv.show_year %}display: table-cell;{% endif %}">{{ cv.get_month_diff_string }}{{ cv.count }}</div>{% endif %}
    </div>

    <div class="position-block">
        <div class="position-header">
            <h2>{{ cv.title }}, </h2>
            <h3><a href="{{ cv.website }}">{{ cv.company }}</a></h3>
        </div>
        <div class="position-meta">
            <span class="entry-date">{{ cv.start_date }}</span>
            {% if cv.active_post %}
                <span class="until-now">{% trans ' until now.' %}</span>
            {% else %}
                <span class="end-date">{% trans ' until' %} {{ cv.end_date }}</span>
            {% endif %}
        </div>
        <div class="position-content">
        <span class="position-description">
            {{ cv.description | safe }}
        </span>
        </div>
    </div>
</div>

{% endfor %}
{% endblock content %}

2 个答案:

答案 0 :(得分:1)

除了您在模板中没有任何名为instance的内容之外,您就像调用它一样调用它。你有一个for循环,它将每个元素分配给一个名为cv的变量,所以你应该使用它。

style="width: {{ cv.get_relative_length }}px;
       height: {{ cv.get_relative_length }}px;
       margin-left: -{% widthratio cv.get_relative_length 2 1 %}px;
       margin-top: -{% widthratio cv.get_relative_length 2 1 %}px;
       "

答案 1 :(得分:0)

模型

  def get_longest_post(self):
        """
        Get the post object with the longest duration
        Returns: longest (amount of months)
        """
        longest = 0
        **for post in CreateCV.objects.all():**

            if post.active_post:
                d2 = datetime.now()
            else:
                d2 = post.end_date

            diff = self.get_month_diff(post.start_date, d2)

            if diff > longest:
                longest = diff

        return longest