显示特定用户django的模型值 [英] showing the model values of specific user django

查看:59
本文介绍了显示特定用户django的模型值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to show the data of a user which he has entered. This is my model

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>

这就是我如何显示模型值

thats how i am showing my model values

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

urlpatterns = patterns('',


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

emp1.html

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

未显示特定的用户值即显示了我一切。
谁能告诉我如何显示特定的用户值/数据吗?

It is not displaying specific user value means it is displaying me everything. can anyone tell me how to show specific user values/data?

推荐答案

您需要更新模型,以便它包含一个用于存储用户的字段-

You need to update your model so that it contains a field to store the user -

from django.contrib.auth.models import User

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

然后您需要创建一个 ModelForm 此处所述。

Then you need to create a ModelForm as described here.

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

Now update your view so that that you use your new 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视图中你做的事-

Now in your object_list view you do something like -

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

这篇关于显示特定用户django的模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆