2

我想显示他输入的用户的数据。这是我的模型

class IgaiaContent(models.Model):
    CONTENT_CHANNELS = (
        ('YouTube','Youtube'),
        ('FaceBook','FaceBook'),
        ('Flickr','Flickr'),
        ('Instagram','Instagram'),
    )
    content_name =          models.CharField(max_length=255, primary_key=True)
    content_type =          models.CharField(max_length=255,null=True)
    content_source =        models.CharField(max_length=255,null=True, choices=CONTENT_CHANNELS)
    content_location =      models.CharField(max_length=255,null=True)
    content_latitude =      models.DecimalField(max_digits=20,decimal_places=2,null=True)
    content_longitude =     models.DecimalField(max_digits=20,decimal_places=2,null=True)
    content_embed_code =    models.TextField(null=True)
    content_description =   models.TextField(null=True)
    content_tags_user  =  models.CharField(max_length=255,null=True)  
    content_time_uploaded = models.DateTimeField(auto_now_add=True)
    content_time_updated =  models.DateField(null=True)

    def __unicode__(self):
        return self.content_name
        return self.content_type
        return self.content_source
        return self.content_location
        return self.content_latitude
        return self.content_longitude
        return self.embed_code
        return self.description
        return self.tags_user 
        return self.time_uploaded
        return self.time_updated



tagging.register(IgaiaContent)

我的观点

def create_page(request):
    if request.method == 'POST':
            form = AuthorForm1(request.POST) 
            if form.is_valid(): 
                    form.save()
                    return HttpResponseRedirect('/thanks/')
    else:
            form = AuthorForm1()

    c = {}
    c.update(csrf(request))
    return render_to_response('portal/form1.htm',{'form':form},context_instance=RequestContext(request))

我的表单模板:

<form method="post" style="height: 553px; width: 594px">
<div class="style12">

{% csrf_token %}

        </br>{{ form.as_p }}

</div>
</form>

这就是我展示我的模型值的方式

employee_info1 = {
    "queryset" : IgaiaContent.objects.all(),
    "template_name" : "portal/emp1.html",
}

urlpatterns = patterns('',


    (r'^view5/',  list_detail.object_list, employee_info1),
)

emp1.html

{% if object_list %}
<table>
<ul>
{% for item in object_list %}
   <li>{{item.content_name}}</li>
   <li>{{item.content_type}}</li>
   <li>{{item.content_source}}</li>
   <li>{{item.content_location}}</li>
   <li>{{item.content_latitude}}</li>
   <li>{{item.content_longitude}}</li>
   <li>{{item.content_embed_code}}</li>
   <li>{{item.content_description}}</li>
   <li>{{item.content_tags_user}}</li>
   <li>{{item.content_time_uploaded}}</li>
   <li>{{item.content_time_updated}}</li></ul>
{% empty %}
   <td colspan="11">No items.</td>
{% endfor %}
</table>
{% endif %}

它没有显示特定的用户价值意味着它正在向我显示所有内容。谁能告诉我如何显示特定的用户值/数据?

4

1 回答 1

3

您需要更新模型,使其包含一个存储用户的字段 -

from django.contrib.auth.models import User

class IgaiaContent(models.Model):
    #...
    user = models.ForeignKey(User)

然后,您需要按照此处所述创建一个ModelForm

class IgaiaContentForm(forms.ModelForm):

   def __init__(self, *args, **kwargs):
       self.request = kwargs.pop('request', None)
       return super(MyModelForm, self).__init__(*args, **kwargs)

   def save(self, *args, **kwargs):
       kwargs['commit']=False
       obj = super(MyModelForm, self).save(*args, **kwargs)
       if self.request:
           obj.user = self.request.user
       obj.save()

   class Meta:
        model = IgaiaContent

现在更新您的视图,以便您使用新的 ModelForm

def create_page(request):
    if request.method == 'POST':
            form = IgaiaContentForm(request.POST) 
            if form.is_valid(): 
                    form.save()
                    return HttpResponseRedirect('/thanks/')
    else:
            form = IgaiaContentForm() 
    #...

现在在您的 object_list 视图中,您可以执行以下操作 -

from django.shortcuts import render_to_response

def object_list(request):
    #....
    object_list = IgaiaContent.objects.filter(user=request.user)
    return render_to_response('object_list_template.html', {'object_list': object_list})
于 2013-02-12T07:47:16.110 回答