为什么模板中注入的数据只有在用户登录后才可用?想要全部 public
why the data injected in template is only available if user is logged In? want all public
我正在开发一个带有关于页面的 portfolio 网站。 模型 是在 DB 上创建的,模板标记正在工作,但只要用户在管理页面中经过身份验证。我已经 将用户模型扩展 为 userprofile 一个,以显示存储在数据库中的投资组合数据 - 显然我希望这是 public 给大家,但我无法得到它。我还想用 superuser 管理 all 与应用 just 相关的模型,因为我有无需创建更多用户,因为 是一个针对单个用户的简单投资组合 。
代码:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField(max_length=1000, blank=True)
location = models.CharField(max_length=30, blank=True)
avatar = models.ImageField(upload_to='profile/', null=True, blank=True)
uploaded_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "{} Portfolio ".format(self.user)
def create_user_profile(sender, instance, created, **kwargs):
"""Create the UserProfile when a new User is saved"""
if created:
profile = UserProfile()
profile.user = instance
profile.save()
post_save.connect(create_user_profile, sender=User)
# ########################################################################################
from coltapp.models import Post, Comment, UserProfile
from django.views.generic import (TemplateView, ListView, DetailView,
CreateView, UpdateView, DeleteView)
class AboutView(ListView):
model = UserProfile
template_name = 'coltapp/about.html'
select_related = ('userprofile')
# ########################################################################################
from coltapp import views
from django.conf.urls import url
#
#
app_name = 'coltapp'
urlpatterns = [
url(r'^about/$', views.AboutView.as_view(), name='about'),
]
# ########################################################################################
<div class = "separator" >
<p align = "center" > User: {{user.get_username}} < /p >
<h3 align = "center" > Bio < /h3 >
<p align = "center" > {{user.userprofile.bio}} < /p >
<p align = "center" > {{user}} < /p >
<p align = "center" > {{object.userprofile.bio}} < /p >
< / div >
# ########################################################################################
粘贴站:
这是因为你默认使用{{ user }}
django 模板。
Django 内置的上下文处理器默认提供 user
,如果未登录,它 returns AnonymousUser
(查看 django 文档 here
因此,如果您在模板中使用 {{ user }}
标签,它会自动在浏览器中显示现在登录的用户 - 浏览器中始终是您,因此您可以在您的投资组合中看到。
1。将您自己的用户传递给上下文
如果您想使用自己的用户并将其显示给任何人(不是登录用户),您可以将自己的用户对象上下文传递给模板。
2。使用您的 UserProfile 对象列表
或者您可以简单地使用 ListView
中的 object_list
:您的投资组合列表都在 UserProfile
对象列表中,对吗?
如果您只有一个用户——就是您——您可以简单地在模板中循环您的 UserProfile 对象。
不使用 {{ user }}
但使用 {{ object_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 }}
。
还有第二个问题,我无法清楚地理解你的问题,但是... object_list
来自你的 AboutView
是你的 UserProfile
模型对象。
Django ListView
自动传递你的模型对象,它使用默认名称 - object_list
。这意味着 object_list
等于 UserProfile.objects.all()
因此,如果您使用 object_list
在模板中进行循环,则所有 UserProfile
对象都在循环。还不清楚吗?
I recommend not using default object_list
. Instead, you can use your own name by adding context_object_name = "profiles"
in AboutView
. Then you can use profiles
in template instead of object_list
. Django Class Based View is really easy, but it's little bit implicated. If you want to know how view-template process work, try using FBV
这是使用 context_object_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
我正在开发一个带有关于页面的 portfolio 网站。 模型 是在 DB 上创建的,模板标记正在工作,但只要用户在管理页面中经过身份验证。我已经 将用户模型扩展 为 userprofile 一个,以显示存储在数据库中的投资组合数据 - 显然我希望这是 public 给大家,但我无法得到它。我还想用 superuser 管理 all 与应用 just 相关的模型,因为我有无需创建更多用户,因为 是一个针对单个用户的简单投资组合 。
代码:
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
class UserProfile(models.Model):
user = models.OneToOneField(User)
bio = models.TextField(max_length=1000, blank=True)
location = models.CharField(max_length=30, blank=True)
avatar = models.ImageField(upload_to='profile/', null=True, blank=True)
uploaded_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return "{} Portfolio ".format(self.user)
def create_user_profile(sender, instance, created, **kwargs):
"""Create the UserProfile when a new User is saved"""
if created:
profile = UserProfile()
profile.user = instance
profile.save()
post_save.connect(create_user_profile, sender=User)
# ########################################################################################
from coltapp.models import Post, Comment, UserProfile
from django.views.generic import (TemplateView, ListView, DetailView,
CreateView, UpdateView, DeleteView)
class AboutView(ListView):
model = UserProfile
template_name = 'coltapp/about.html'
select_related = ('userprofile')
# ########################################################################################
from coltapp import views
from django.conf.urls import url
#
#
app_name = 'coltapp'
urlpatterns = [
url(r'^about/$', views.AboutView.as_view(), name='about'),
]
# ########################################################################################
<div class = "separator" >
<p align = "center" > User: {{user.get_username}} < /p >
<h3 align = "center" > Bio < /h3 >
<p align = "center" > {{user.userprofile.bio}} < /p >
<p align = "center" > {{user}} < /p >
<p align = "center" > {{object.userprofile.bio}} < /p >
< / div >
# ########################################################################################
粘贴站:
这是因为你默认使用{{ user }}
django 模板。
Django 内置的上下文处理器默认提供 user
,如果未登录,它 returns AnonymousUser
(查看 django 文档 here
因此,如果您在模板中使用 {{ user }}
标签,它会自动在浏览器中显示现在登录的用户 - 浏览器中始终是您,因此您可以在您的投资组合中看到。
1。将您自己的用户传递给上下文
如果您想使用自己的用户并将其显示给任何人(不是登录用户),您可以将自己的用户对象上下文传递给模板。
2。使用您的 UserProfile 对象列表
或者您可以简单地使用 ListView
中的 object_list
:您的投资组合列表都在 UserProfile
对象列表中,对吗?
如果您只有一个用户——就是您——您可以简单地在模板中循环您的 UserProfile 对象。
不使用 {{ user }}
但使用 {{ object_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 }}
。
还有第二个问题,我无法清楚地理解你的问题,但是... object_list
来自你的 AboutView
是你的 UserProfile
模型对象。
Django ListView
自动传递你的模型对象,它使用默认名称 - object_list
。这意味着 object_list
等于 UserProfile.objects.all()
因此,如果您使用 object_list
在模板中进行循环,则所有 UserProfile
对象都在循环。还不清楚吗?
I recommend not using default
object_list
. Instead, you can use your own name by addingcontext_object_name = "profiles"
inAboutView
. Then you can useprofiles
in template instead ofobject_list
. Django Class Based View is really easy, but it's little bit implicated. If you want to know how view-template process work, try using FBV
这是使用 context_object_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