.get_object()显示数据集而不是模型中的字段数据

我正在建立一个网站,在页面上显示食谱的食谱。

这是我到目前为止所拥有的

models.py

class cookbook(models.Model):
    title = models.CharField(max_length=255,unique=True)

class ingredient (models.Model):
    name = models.CharField(max_length=255,unique=True)


class recipesteps(models.Model):
    ingredient = models.ForeignKey(ingredient,on_delete=models.CASCADE)
    instructions = models.TextField()
    time =  models.IntegerField(default=0)

class recipe(models.Model):
    name = models.CharField(max_length=255,unique=True)
    cookbook = models.ForeignKey(cookbook,on_delete=models.CASCADE)
    ingredient_used = models.ManyToManyField(ingredient)
    recipe_steps = models.ForeignKey(recipesteps,on_delete=models.CASCADE)
    def __str__(self):
           return 'name={}   cookbook={} `'.format(self.name,self.cookbook)

views.py

from django.views.generic import DetailView

class RecipeDetailView(DetailView):
 model = recipe
     def get_context_data(self,**kwargs):
         context = super(RecipeDetailView,self).get_context_data(**kwargs)
         context['instructions'] = recipesteps.objects.filter(recipe=self.get_object())
return context

template.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>{{ object.cookbook }}</h2>
<h2> Recipe Name = {{ object.name }} </h2>
<h2> Steps To Make:</h2>
 {{ instructions }} 
</body>
</html>

模板中{{指令}}变量的输出为:

<QuerySet [<recipesteps: name=Tomato cookbook=Cooking with tomato >,<recipesteps: name=Lettuce cookbook= Cooking with lettuce >]>

有没有一种方法可以在模板中的某个位置仅显示成分名称,而在不带?的位置显示食谱?

shijy07 回答:.get_object()显示数据集而不是模型中的字段数据

您必须遍历指令对象

using UnityEngine;
using Rewired;

namespace KartGame.KartSystems
{
    /// <summary>
    /// A basic gamepad implementation of the IInput interface for all the input information a kart needs.
    /// </summary>
    public class GamepadInput : MonoBehaviour,IInput
    {
        public float Acceleration
        {
            get { return m_Acceleration; }
        }
        public float Steering
        {
            get { return m_Steering; }
        }
        public bool BoostPressed
        {
            get { return m_BoostPressed; }
        }
        public bool FirePressed
        {
            get { return m_FirePressed; }
        }
        public bool HopPressed
        {
            get { return m_HopPressed; }
        }
        public bool HopHeld
        {
            get { return m_HopHeld; }
        }

        float m_Acceleration;
        float m_Steering;
        bool m_HopPressed;
        bool m_HopHeld;
        bool m_BoostPressed;
        bool m_FirePressed;

        bool m_FixedUpdateHappened;

        void Update ()
        {
            if (Input.GetButton ("Brake"))
                m_Acceleration = -1f;
            else if (Input.GetButton ("Accelerate"))
                m_Acceleration = 1f;
            else
                m_Acceleration = 0f;

            m_Steering = Input.GetAxis ("Horizontal");

            m_HopHeld = Input.GetButton ("Hop");

            if (m_FixedUpdateHappened)
            {
                m_FixedUpdateHappened = false;

                m_HopPressed = false;
                m_BoostPressed = false;
                m_FirePressed = false;
            }

            m_HopPressed |= Input.GetButtonDown ("Hop");
            m_BoostPressed |= Input.GetButtonDown ("Boost");
            m_FirePressed |= Input.GetButtonDown ("Fire");
        }

        void FixedUpdate ()
        {
            m_FixedUpdateHappened = true;
        }
    }
}
,

如果您不想添加额外的上下文,则可以执行以下操作:

function wordWrap(long_string,max_char){
  var sum_length_of_words = function(word_array){
    var out = 0;
    if (word_array.length!=0){
      for (var i=0; i<word_array.length; i++){
        var word = word_array[i];
        out = out + word.length;
      }
    };
    return out;
  }
  var split_out = [[]];
  var split_string = long_string.split(' ');
  for (var i=0; i<split_string.length; i++){
    var word = split_string[i];

    if ((sum_length_of_words(split_out[split_out.length-1]) + word.length) > max_char){
      split_out = split_out.concat([[]]);
    }
    split_out[split_out.length-1] = split_out[split_out.length-1].concat(word);
  }
  for (var i=0; i<split_out.length; i++){
    split_out[i] = split_out[i].join(" ");
  }
  return split_out.join('\n');
};

您的对象已经可以访问指令。

本文链接:https://www.f2er.com/3116014.html

大家都在问