2012-01-24 126 views
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'添加到我的設置文件中,這使我可以看到未找到準確性變量。我可以知道我做錯了什麼嗎?

任何意見將不勝感激!謝謝:)

+0

我不確定你在問什麼。如果是從第三方應用程序的視圖,你不應該自己修改它 - 'extra_context'參數的整點是,你可以在額外的值傳遞從URL配置,這是你會怎麼傳進去'準確性「應該是。 –

+0

@DanielRoseman謝謝你的評論,我完全理解你的意思:)我想這個URL(R '^(P \ w +)/ $?', views.profile_detail,extra_context中用= { '精度': '土豆' },name ='profiles_profile_detail'),這是否意味着我應該能夠直接使用變量{{accuracy}},因爲它似乎沒有工作。再次感謝! – nightscent

回答

0

唉我發現了問題!我正在更改錯誤的文件> _ <,因爲我的python安裝已經寫入默認目錄。