2016-11-15 28 views
0

iam創建一個cv應用程序,但不知道如何從模板獲得 get_relative_length函數, 數據不通過,我需要幫助,iam新增到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" 

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。你有一個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; 
     " 
+0

首先,我感謝你的幫助,這就是我如何準確地試了一下第一,原因是有道理的我,但後來這個錯誤顯示出來: 全局名稱「郵報」沒有定義@Daniel Roseman –

+0

這似乎是一個完全不同的問題。您在'get_longest_post'方法中使用了Post,但是您尚未在任何地方定義或導入它。儘管看起來這種方法看起來不應該在CreateCV上生存,因爲它根本沒有引用該模型。 –

+0

謝謝,我想通了,你是對的,我在get_longest_post方法中將「Post」更改爲「CreateCV」,現在它起作用了 –

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