I have this line of html code
Calculate distance {{distance}}
but instead of grabbing the information from the object, it instead displays
Calculate distance Measurements object (1)
This is my model
class Measurements(models.Model):
location = models.CharField(max_length=200)
destination = models.CharField(max_length=200)
distance = models.DecimalField(max_digits=10, decimal_places=2)
created = models.DateTimeField(auto_now_add=True)
forms.py
from django import forms
from .models import Measurements
class MeasuremeantModelForm(forms.ModelForm):
class Meta:
model = Measurements
fields = ('distance'),
views.py
from django.shortcuts import render, get_object_or_404
from .models import Measurements
# Create your views here.
def calculate_distance_view(request):
obj = get_object_or_404(Measurements)
context = {
'distance' : obj,
}
return render(request, 'measurements/main.html', context)
I can't figure out why it wouldn't be displaying the contents of the object the object's distance has a value of 2000
CodePudding user response:
distance
is a Measurements
object, in order to render the distance of that object, you render it with:
Calculate distance {{ distance.distance }}
You can also override the __str__
method to specify how to represent an object as text. For example:
class Measurements(models.Model):
location = models.CharField(max_length=200)
destination = models.CharField(max_length=200)
distance = models.DecimalField(max_digits=10, decimal_places=2)
created = models.DateTimeField(auto_now_add=True)
def __str__(self):
'from {self.location} to {self.destination} is {self.distance}'