Python 为什么模板中注入的数据只有在用户登录时才可用?想要所有的公众

Python 为什么模板中注入的数据只有在用户登录时才可用?想要所有的公众,python,django,django-models,django-templates,web-development-server,Python,Django,Django Models,Django Templates,Web Development Server,我正在一个公文包网站上工作,有一个关于页面。模型是在数据库上创建的,模板标记正在工作,但前提是用户在管理页面中通过了身份验证。我已经将用户模型扩展为用户配置文件一个,以显示数据库中存储的投资组合数据-显然,我希望这对每个人都是公开的,但我无法获得它。另外,我想通过超级用户管理与应用程序相关的所有型号,因为我不需要创建更多用户,因为是一个简单的单一用户组合 代码: 来自django.db导入模型的 从django.contrib.auth.models导入用户 从django.db.models

我正在一个公文包网站上工作,有一个关于页面。模型是在数据库上创建的,模板标记正在工作,但前提是用户在管理页面中通过了身份验证。我已经用户模型扩展为用户配置文件一个,以显示数据库中存储的投资组合数据-显然,我希望这对每个人都是公开的,但我无法获得它。另外,我想通过超级用户管理与应用程序相关的所有型号,因为我不需要创建更多用户,因为是一个简单的单一用户组合

代码:

来自django.db导入模型的

从django.contrib.auth.models导入用户
从django.db.models.signals导入后保存
类UserProfile(models.Model):
用户=模型。OneToOneField(用户)
bio=models.TextField(最大长度=1000,空白=True)
位置=型号.CharField(最大长度=30,空白=真)
avatar=models.ImageField(上传到='profile/',null=True,blank=True)
上传的\u at=models.datetime字段(自动\u now\u add=True)
定义(自我):
返回{}Portfolio.format(self.user)
def create_user_配置文件(发送方、实例、已创建、**kwargs):
“”“保存新用户时创建用户配置文件”“”
如果创建:
profile=UserProfile()
profile.user=实例
profile.save()
post\u save.connect(创建用户配置文件,发件人=用户)
# ########################################################################################
从coltapp.models导入Post、Comment、UserProfile
从django.views.generic导入(TemplateView、ListView、DetailView、,
CreateView、UpdateView、DeleteView)
类AboutView(列表视图):
model=UserProfile
模板名称='coltapp/about.html'
选择_related=('userprofile')
# ########################################################################################
从COLTAP导入视图
从django.conf.url导入url
#
#
应用程序名称='coltapp'
URL模式=[
url(r“^about/$”,views.AboutView.as_view(),name='about'),
]
# ########################################################################################

用户:{{User.get_username}}

生物技术

{{user.userprofile.bio}

{{user}

{{{object.userprofile.bio}}

# ######################################################################################## 巴斯德宾:


这是因为默认情况下使用的是
{{user}

默认情况下,Django的内置上下文处理器提供
用户
,如果未登录,则返回
匿名用户
(检查Django文档

所以,如果您在模板中使用
{{user}}
标记,它会自动在浏览器中显示现在登录的用户,并且浏览器中始终是您,因此您可以在公文包中查看

1.将您自己的用户传递到上下文 若您想使用自己的用户并将其显示给任何人(非登录用户),则可以将自己的用户对象上下文传递给模板

2.使用用户配置文件对象列表 或者你可以简单地使用
列表视图中的
对象列表
:你的投资组合列表都在
用户档案
对象列表中,对吗

若你们只有一个用户——就是你们——你们可以简单地在模板中循环你们的UserProfile对象

不使用
{{user}}
而是使用
{{{object\u list}}
进行循环

如果您有更多问题,请留下评论

更新 这是使用上下文数据传递您自己的模型的简单示例

from django.contrib.auth.models import User

class AboutView(ListView):

    model = UserProfile
    template_name = 'coltapp/about.html'
    select_related = ('userprofile')

    def get_context_data(self, **kwargs):
        context = super(AboutView, self).get_context_data(**kwargs)
        # just filter your user by username, email, pk...
        my_user = User.objects.get(username="your_username")
        context[my_user] = my_user
        return context
然后可以在模板中使用
{{my_user}}

关于第二个问题,我无法清楚地理解您的it,但您的
关于视图的
对象列表
是您的
用户配置文件
模型对象

Django
ListView
自动传递模型对象,并使用默认名称-
object\u list
。这意味着
object\u list
等于
UserProfile.objects.all()

所以,如果您使用
object\u list
在模板中进行forloop,那么您的所有
UserProfile
对象都在循环。不是很清楚吗

我建议不要使用默认的
object\u list
。相反,您可以通过添加
context\u object\u name=“profiles”来使用自己的名称
AboutView
中。然后您可以在模板中使用
配置文件
而不是
对象列表
。基于Django类的视图非常简单,但有点牵连。如果您想知道视图模板过程是如何工作的,请尝试使用FBV

这是使用
context\u object\u name

class AboutView(ListView):

    model = UserProfile
    template_name = 'coltapp/about.html'
    context_object_name = 'profiles'
    select_related = ('userprofile')

    def get_context_data(self, **kwargs):
        context = super(AboutView, self).get_context_data(**kwargs)
        # just filter your user by username, email, pk...
        my_user = User.objects.get(username="your_username")
        context[my_user] = my_user
        return context

嘿,谢谢你的留言,你能给我一个第一步的例子吗?传递你自己的用户上下文..关于你在第二条中说的,是的,我的投资组合列表在UserProfile中,我只有一个用户,我的用户。我会按照你的回答检查,我只是阅读你链接中的文档,但是如果你能给我一个代码片段,我会很高兴nkful(甚至更多)我已经尝试了循环解决方案,它工作了,因为我只有一个模型,只有Django用户与userProfile的关系,很好,但是如果我想直接访问这个用户配置文件或任何对象,比如:{object_list.portfolio}或{{objectt_list.carousel}?我将编辑我的答案,并通过你自己的用户上下文。但我不明白第二点…你说的“直接访问”是什么意思?什么是“{object_list.carousel}”?我在阅读这里的文档时发现了有用的信息,按照你的指示,我无论如何都会接受你的答案,因为它有助于引导我了解这些内容
class AboutView(ListView):

    model = UserProfile
    template_name = 'coltapp/about.html'
    context_object_name = 'profiles'
    select_related = ('userprofile')

    def get_context_data(self, **kwargs):
        context = super(AboutView, self).get_context_data(**kwargs)
        # just filter your user by username, email, pk...
        my_user = User.objects.get(username="your_username")
        context[my_user] = my_user
        return context