2016-11-15 10 views
0

iam cv appを作成していますが、 テンプレートのモデルからget_relative_length関数を取得する方法がわかりません データが邪魔にならず、私は助けが必要です。 私は本当にあなたの助けテンプレート内のモデルから関数を取得

モデル

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" 

templa感謝TE

{% 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 %} 

答えて

1

あなたは、あなたがテンプレートでinstanceと呼ばれるものを持っていないことを除いて、行っているだけのようにそれを呼び出します。それぞれの要素にcvという変数を割り当てるforループがあるので、それを使用する必要があります。

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; 
     " 
+0

私はあなたの助けに感謝し、私はまさにそれを最初に試みたが、原因は私のために意味を作った方法thatsのが、その後、このエラーが現れます: グローバル名「ポスト」が定義されていません@ダニエル・ローズマン –

+0

それはまったく別の問題のようです。 'get_longest_post'メソッドでPostを使用していますが、どこにも定義していないか、インポートしていません。とにかくCreateCVにはそのメソッドが存在していないように見えますが、それはそのモデルをまったく参照していないからです。 –

+0

ありがとう、私はそれを理解した、あなたは正しかった、私は "CreateCV"に "投稿"、get_longest_postメソッドで変更され、今は動作します –

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 
関連する問題