Reputation: 3215
The problem i am having is I can't access a model's fields in the html {{ profile.slug }}
. In the template I can access the model, let me show you.
I have 2 models Profile, and Oferto. In Detail view on the Oferto Model, I want to link to the user's profile who created the Oferto. the Oferto Model has a field user,
I am trying to lookup the profile.slug
that coresponds to the Oferto.user
The following is seen in the browser from a test
[<Profile: Red>]
Name: oFFER
User: Red
Description:
Time: 9
Stelo: None
Requirements:9
and the template is as follows
{% block content %}
<a>{{ profile }}</a>
<p>Name: {{ oferto.name }}</p>
<p>User: {{ oferto.user }}</p>
<p>Description: {{ oferto.descripion }}</p>
<p>Time: {{ oferto.time }}</p>
<p>Stelo: {{ oferto.stelo }}</p>
<p>Requirements:{{ oferto.requirements }}</p>
<hr>
<p>Location: {{ oferto.location }}</p>
<p>tags: {{ oferto.tags }}</p>
<p>{{ PROJECT_URL }} / {{ STATIC_URL }}{{ oferto.image }}</p>
{% endblock %}
if i try to use profile.slug
it just comes up blank and is not in the html
views.py
class OfertoDetailView(ExtraContextMixin,DetailView):
model = Oferto
def extra(self):
profile = Profile.objects.all()
return dict(profile = profile)
class ExtraContextMixin(object):
def get_context_data(self, **kwargs):
context = super(ExtraContextMixin, self).get_context_data(**kwargs)
context.update(self.extra())
return context
def extra(self):
return dict()
if your wondering why I am using a mixin see an answer from django - DetailView how to display two models at same time
My Models
# Ofertoj.models.py
class Oferto(models.Model):
user = models.ForeignKey(User)
# profile = models.OneToOneField(Profile)
name = models.CharField(max_length=150)
description = models.TextField(max_length=3000)
time = models.DecimalField(max_digits= 10000000,decimal_places =2,null= True)
stelo = models.DecimalField(max_digits= 10000000,decimal_places =2,null= True)
location = models.TextField(max_length=3000)
slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))
tags = tagging.fields.TagField()
image = models.ImageField(upload_to='Ofertoj',blank=True, null=True)
requirements = models.TextField(max_length=550000,blank=True, null=True)
def get_absolute_url(self):
return reverse('oferto_detail', kwargs={'slug': self.slug})
def __unicode__(self):
return self.name
def get_tags(self):
return Tag.objects.get_for_object(self)
# turtle.models.py
class BaseInfo(models.Model):
name = models.CharField(max_length=100)
contact_name = models.CharField(max_length=100)
email = models.EmailField(max_length=75)
phone = models.CharField(max_length=20)
address = models.CharField(max_length=3000)
city = models.CharField(max_length=3000)
state = models.CharField(max_length=3000)
code = models.IntegerField()
country = models.CharField(max_length=3000)
image = models.ImageField(upload_to='photos/%Y/%m/%d',blank=True)
slug = AutoSlugField(('slug'), max_length=128, unique=True, populate_from=('name',))
tags = tagging.fields.TagField()
def __unicode__(self):
return self.name
def get_tags(self):
return Tag.objects.get_for_object(self)
# profile.models.py
class Profile(BaseInfo):
bio = models.TextField(max_length=15000000)
user = models.ForeignKey(User)
def get_absolute_url(self):
return reverse('profile_detail', kwargs={'slug': self.slug})
# tempilo.profiles.UserProfiles
from models import Profile
class MyUser(AbstractBaseUser):
identifier = models.CharField(max_length=40, unique=True)
USERNAME_FIELD = 'identifier'
profile = OneToOneField(Profile,primary_key=True)
Upvotes: 0
Views: 396
Reputation: 599490
profile
is a queryset, not an instance. Querysets don't have a slug
attribute.
You either need to get a specific instance of Profile in your extra method, or iterate through the profiles in your template.
Upvotes: 1
Reputation: 16226
The fact that [<Profile: Red>]
is visible in the test output indicates that you are passing Profile
instance to the template correctly.
Therefore the problem lies within profile.slug
. Are you sure that you have slug
field on your Profile
class?
Upvotes: 0