2014-02-25 113 views
0

我遇到的問題是我無法訪問html {{ profile.slug }}中的模型字段。在模板中,我可以訪問模型,讓我給你看。訪問模型字段DetailView

我有2個模型Profile和Oferto。 在Oferto模型的詳細視圖中,我想鏈接到創建Oferto的用戶配置文件。 的Oferto型號有一個字段的用戶,

我試圖查找該coresponds到Oferto.user

profile.slug在瀏覽器中看到從測試

[<Profile: Red>] 
Name: oFFER 

User: Red 

Description: 

Time: 9 

Stelo: None 

Requirements:9 

及以下模板是如下

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

,如果我嘗試使用profile.slug它只是出現空白,而不是在H TML

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() 

如果你想知道爲什麼我使用一個混合看到 django - DetailView how to display two models at same time

我的模型

# 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) 

回答

1

profile是一個查詢集,而不是一個實例。查詢集沒有slug屬性。

您或者需要在您的額外方法中獲取配置文件的特定實例,或者遍歷模板中的配置文件。

+0

好吧所以我知道我可以得到一個用戶:紅色,所以我如何得到用戶的一個實例:紅色 – Klanestro

+0

你不顯示你的模型,所以很難提供幫助。如果在ofero和用戶和配置文件之間存在關係,那麼您根本不需要額外的方法:您可以在模板中遵循該關係(例如'oferto.user.profile') - 但我們需要看到模型準確地說明如何去做。 –

+0

添加了幾個模型 – Klanestro

0
答案

事實上,[<Profile: Red>]是測試輸出可見表明你是正確地將Profile實例傳遞給模板。

因此問題在於profile.slug。您確定您的Profile課程中有slug字段嗎?