【发布时间】:2012-05-09 17:09:50
【问题描述】:
我正在使用 django-registration 和 django-profiles 构建应用程序。在页面的任何地方,我都有显示当前登录用户数据的部分(如名字和姓氏,使用如下语法:{{ user.first_name }}),它工作正常。它是通过 子页面模板来扩展一个具有 HTML 结构的主模板 和提到的部分来完成的。
现在我尝试将用户图像(使用{{ profile.image }})添加到该部分,但在以下页面上,{{ profile }} 模板变量的可用性存在问题:
- http://127.0.0.1:8000/profiles/myusername/(其中 myusername 是有头像的人的用户名),
- http://127.0.0.1:8000/profiles/edit/。
在 settings.py 我有:
AUTH_PROFILE_MODULE = 'intranet.UserProfile'
TEMPLATE_CONTEXT_PROCESSORS = (
'django.core.context_processors.static',
)
将"django.contrib.auth.context_processors.auth", 添加到TEMPLATE_CONTEXT_PROCESSORS 不会改变任何内容。
我在 models.py 中的 UserProfile 类是:
class UserProfile(models.Model):
user = models.OneToOneField(User)
image = models.ImageField(upload_to=user_image_name,
blank=True, null=True, verbose_name='User photo')
urls.py:
(r'^profiles/edit/', 'profiles.views.edit_profile', {'form_class': ProfileForm, }),
(r'^profiles/', include('profiles.urls')),
所以其余的默认设置在 django-profiles urls.py 文件中。
我希望能够在应用程序的模板中使用{{ profile }}随处可见的模板变量(不仅在个人资料页面上),以便能够在其他人扩展的主模板中使用它。
请告诉我如何获得这个。任何帮助将不胜感激。
我在 1.3.1 版中使用 Django,在 0.7 版中使用 django-registration,在 0.2 版中使用 django-profiles。
【问题讨论】:
标签: django templates django-templates profiles django-profiles