0
Django新手在工作,我可以使用一些指针。我使用的是django-profile,并且一直在处理由views.profile_detail处理的我的个人资料页面。我面临的问题是我无法使用此视图在我的模板中添加另一个变量。这是我的看法功能:django-profile将变量传递给模板
def profile_detail(request, username, public_profile_field=None,
template_name='profiles/profile_detail.html',
extra_context=None):
"""
Detail view of a user's profile.
If no profile model has been specified in the
``AUTH_PROFILE_MODULE`` setting,
``django.contrib.auth.models.SiteProfileNotAvailable`` will be
raised.
If the user has not yet created a profile, ``Http404`` will be
raised.
**Required arguments:**
``username``
The username of the user whose profile is being displayed.
**Optional arguments:**
``extra_context``
A dictionary of variables to add to the template context. Any
callable object in this dictionary will be called to produce
the end result which appears in the context.
``public_profile_field``
The name of a ``BooleanField`` on the profile model; if the
value of that field on the user's profile is ``False``, the
``profile`` variable in the template will be ``None``. Use
this feature to allow users to mark their profiles as not
being publicly viewable.
If this argument is not specified, it will be assumed that all
users' profiles are publicly viewable.
``template_name``
The name of the template to use for displaying the profile. If
not specified, this will default to
:template:`profiles/profile_detail.html`.
**Context:**
``profile``
The user's profile, or ``None`` if the user's profile is not
publicly viewable (see the description of
``public_profile_field`` above).
**Template:**
``template_name`` keyword argument or
:template:`profiles/profile_detail.html`.
"""
user = get_object_or_404(User, username=username)
# accuracy = ''
try:
profile_obj = user.get_profile()
accuracy = str(profile_obj.number_of_answers/profile_obj.number_of_answers) + '%'
except ObjectDoesNotExist:
raise Http404
if public_profile_field is not None and \
not getattr(profile_obj, public_profile_field):
profile_obj = None
if extra_context is None:
# extra_context = {'accuracy': potato}
extra_context = {}
context = RequestContext(request)
# context['accuracy'] = 'potato'
for key, value in extra_context.items():
context[key] = callable(value) and value() or value
return render_to_response(template_name,
{'profile': profile_obj},
# { 'profile': profile_obj, 'accuracy': accuracy},
# locals(),
context_instance=context)
,这里是我的模板:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<p><strong>Level:</strong><br>{{ profile.level }}</p>
<p><strong>Bounty Points:</strong><br>{{ profile.total_bounty_points }}</p>
<p><strong>Number of questions:</strong><br>{{ profile.number_of_questions_asked }}</p>
<p><strong>Number of replies:</strong><br>{{ profile.number_of_replies }}</p>
<p><strong>Number of answers:</strong><br>{{ profile.number_of_answers }}</p>
<p><strong>Accuracy:</strong><br>{{ accuracy }}</p>
<p><strong>Number of times reported:</strong><br>{{ profile.reported_by_others }}</p>
{% endblock %}
我想知道其中的值的个人资料正在从通过呢?它来自词典{'profile':profile_obj}还是来自上下文?我试着评论这两个,但模板仍然呈现罚款。
我也尝试在我的模板中创建一个名为精度的新变量,但我无法让它呈现,模板只是简单地失败。然后,我将TEMPLATE_STRING_IF_INVALID ='%s'添加到我的设置文件中,这使我可以看到未找到准确性变量。我可以知道我做错了什么吗?
任何意见将不胜感激!谢谢:)
我不确定你在问什么。如果是从第三方应用程序的视图,你不应该自己修改它 - 'extra_context'参数的整点是,你可以在额外的值传递从URL配置,这是你会怎么传进去'准确性“应该是。 –
@DanielRoseman谢谢你的评论,我完全理解你的意思:)我想这个URL(R '^(P \ w +)/ $?', views.profile_detail,extra_context中用= { '精度': '土豆' },name ='profiles_profile_detail'),这是否意味着我应该能够直接使用变量{{accuracy}},因为它似乎没有工作。再次感谢! –
nightscent