如何在呈现HTML页面时使用与Django中的用户关联的模型

时间:2022-02-14 03:31:55

I'm in the learning stages of django. I just dived into a project to learn the framework and am having a series of questions throughout the process.

我正处于django的学习阶段。我只是潜入一个项目来学习框架,并在整个过程中遇到一系列问题。

I basically want to have individual pages for users who create a task list and have them post tasks to their own page.

我基本上希望为创建任务列表的用户提供单独的页面,并让他们将任务发布到他们自己的页面。

from django.db import models from django.contrib.auth.models import User

来自django.db的导入模型来自django.contrib.auth.models导入用户

# Create your models here.
class UserProfile(models.Model):
    # This line is required. Links UserProfile to a User model instance.
    user = models.OneToOneField(User)
    # The additional attributes we wish to include.
    website = models.URLField(blank = True)
    # Override the __unicode__() method to return out something meaningful!
    def __unicode__(self):
        return self.user.username

class TaskItem(models.Model):
    taskn = models.CharField(max_length = 400)
    usern = models.ForeignKey(User)

In my template, if the user has their task entered, how do I call it to render onto the page?

在我的模板中,如果用户输入了他们的任务,我该如何调用它来渲染到页面上?

My View:

def profile_page(request, username):
    user = User.objects.get(username=username)
    taskitems = user.taskn_set.all()
    return render_to_response('profile.html', {}, context)

Current issue:

当前的问题:

'User' object has no attribute 'taskn_set'

2 个解决方案

#1


1  

{{ request.user.taskitem_set.all }} would give you all the related task items. Now, to display it in your template:

{{request.user.taskitem_set.all}}会为您提供所有相关的任务项。现在,要在模板中显示它:

{% for task_item in user.taskitem_set.all %}
    {{ task_item.task_n }}
{% endfor %}

would display the list of tasks.

将显示任务列表。

Here is the documentation on reverse-queries on foreign key (related_name) Also, read this

这是关于外键反向查询的文档(related_name)另外,请阅读此内容

#2


0  

you would do something like this:

你会做这样的事情:

{% for task in user.taskitem_set.all %}
    {{ task.task_n }}
{% endfor %}

This will fetch all TaskItem instances related to your user. (notice the extra database query)

这将获取与您的用户相关的所有TaskItem实例。 (注意额外的数据库查询)

While i don't know how your view works, i will assume that you are making the right checks to make sure that every user can only see his own tasks.

虽然我不知道您的视图是如何工作的,但我会假设您正在进行正确的检查以确保每个用户只能看到自己的任务。

One performance trick you will find most useful is to use prefetch_related('taskitem_set'), this will prefetch the TaskItem instances as long as your UserProfile instance with one query:

您将发现最有用的一个性能技巧是使用prefetch_related('taskitem_set'),只要您的UserProfile实例具有一个查询,这将预取TaskItem实例:

user = User.objects.filter(id=user_id).prefetch_related('taskitem_set')

You can tune the code to match your preferences.

您可以调整代码以符合您的偏好。

Hope this helps!

希望这可以帮助!

#1


1  

{{ request.user.taskitem_set.all }} would give you all the related task items. Now, to display it in your template:

{{request.user.taskitem_set.all}}会为您提供所有相关的任务项。现在,要在模板中显示它:

{% for task_item in user.taskitem_set.all %}
    {{ task_item.task_n }}
{% endfor %}

would display the list of tasks.

将显示任务列表。

Here is the documentation on reverse-queries on foreign key (related_name) Also, read this

这是关于外键反向查询的文档(related_name)另外,请阅读此内容

#2


0  

you would do something like this:

你会做这样的事情:

{% for task in user.taskitem_set.all %}
    {{ task.task_n }}
{% endfor %}

This will fetch all TaskItem instances related to your user. (notice the extra database query)

这将获取与您的用户相关的所有TaskItem实例。 (注意额外的数据库查询)

While i don't know how your view works, i will assume that you are making the right checks to make sure that every user can only see his own tasks.

虽然我不知道您的视图是如何工作的,但我会假设您正在进行正确的检查以确保每个用户只能看到自己的任务。

One performance trick you will find most useful is to use prefetch_related('taskitem_set'), this will prefetch the TaskItem instances as long as your UserProfile instance with one query:

您将发现最有用的一个性能技巧是使用prefetch_related('taskitem_set'),只要您的UserProfile实例具有一个查询,这将预取TaskItem实例:

user = User.objects.filter(id=user_id).prefetch_related('taskitem_set')

You can tune the code to match your preferences.

您可以调整代码以符合您的偏好。

Hope this helps!

希望这可以帮助!