我有以下代码,呈现的模板没有显示任何输入,使用的代码是
views.py
@login_required
def settings(request, profile_type=None):
context = {}
initial_data = dict()
setting_object = dict()
settings = EmailEvent.objects.all()
for setting in settings:
tmp_settings_object = EmailSetting.objects.filter(user=request.user, event=setting)
if not tmp_settings_object:
setting_object[setting], created = EmailSetting.objects.get_or_create(user=request.user, event=setting)
initial_data[setting.name] = setting_object[setting].enabled
else:
setting_object[setting] = tmp_settings_object[0]
initial_data[setting.name] = setting_object[setting].enabled
email_settings_form = EmailSettingsForm(initial=initial_data)
password_form = PasswordChangeForm()
profile = request.user.get_profile().profile(request)
if not profile:
return HttpResponseRedirect('/signup/additional/')
profile_type = profile.get_type().lower()
user_info_form = UserInfoForm()
if request.method == 'POST':
if request.POST.get('old_password'):
password_form = PasswordChangeForm(request.POST)
if password_form.is_valid():
[cd, error] = password_form.clean_data(request.user)
if error:
messages.error(request, error)
else:
request.user.set_password(password_form.cleaned_data['new_password'])
request.user.save()
messages.success(request, 'Your password has been changed.')
context['password_form'] = password_form
else:
user_info_form = UserInfoForm(request.POST, request.FILES, request=request)
if user_info_form.is_valid():
user_info_form.save(request.user, profile_type)
messages.success(request, 'You have successfully updated your profile.')
if request.is_ajax():
response = simplejson.dumps({"status": "Upload Success"})
return HttpResponse(response, mimetype='application/json')
return HttpResponseRedirect(request.POST.get('next', '/profile/' + profile_type + '/' + request.user.username + '/'))
else:
initial = {}
initial['first_name'] = request.user.first_name
initial['last_name'] = request.user.last_name
initial['email'] = request.user.email
if profile:
initial['phone_number'] = profile.phone_number
initial['about'] = profile.about
initial['country'] = profile.country
initial['job_title'] = profile.job_title
firms = profile.works_firms()
if firms:
context['firm'] = firms[0]
if profile_type == "investor":
initial['investor_type'] = profile.investor_type
user_info_form = UserInfoForm(initial=initial)
context['user_info_form'] = user_info_form
context['profile_type'] = profile_type
context['content_type'] = ContentType.objects.get_for_model(profile)
#end of edit profile
try:
context['subscription'] = Subscription.objects.get(user=request.user)
except ObjectDoesNotExist:
None
if request.user.get_profile().has_both_profiles():
investor_profile = InvestorProfile.objects.get(user=request.user)
investor_settings, created = InvestorPrivacySetting.objects.get_or_create(profile=investor_profile)
context['investor_form'] = InvestorPrivacySettingForm(prefix='investor', instance=investor_settings)
manager_profile = ManagerProfile.objects.get(user=request.user)
manager_settings, created = ManagerPrivacySetting.objects.get_or_create(profile=manager_profile)
context['manager_form'] = ManagerPrivacySettingForm(prefix='manager', instance=manager_settings)
investor_activity_settings = []
for action in Action.objects.all().exclude(privacy_settings_text__isnull=True).exclude(privacy_settings_text=''):
setting, created = ActivityPrivacySetting.objects.get_or_create(profile_content_type=ContentType.objects.get_for_model(investor_profile), profile_object_id=investor_profile.id, action=action)
investor_activity_settings.append(setting)
context['investor_activity_settings'] = investor_activity_settings
manager_activity_settings = []
for action in Action.objects.all().exclude(privacy_settings_text__isnull=True).exclude(privacy_settings_text=''):
setting, created = ActivityPrivacySetting.objects.get_or_create(profile_content_type=ContentType.objects.get_for_model(manager_profile), profile_object_id=manager_profile.id, action=action)
manager_activity_settings.append(setting)
context['manager_activity_settings'] = manager_activity_settings
elif request.session['role'] == 'investor':
investor_profile = InvestorProfile.objects.get(user=request.user)
investor_settings, created = InvestorPrivacySetting.objects.get_or_create(profile=investor_profile)
context['investor_form'] = InvestorPrivacySettingForm(prefix='investor', instance=investor_settings)
investor_activity_settings = []
for action in Action.objects.all().exclude(privacy_settings_text__isnull=True).exclude(privacy_settings_text=''):
setting, created = ActivityPrivacySetting.objects.get_or_create(profile_content_type=ContentType.objects.get_for_model(investor_profile), profile_object_id=investor_profile.id, action=action)
investor_activity_settings.append(setting)
context['investor_activity_settings'] = investor_activity_settings
elif request.session['role'] == 'manager':
manager_profile = ManagerProfile.objects.get(user=request.user)
manager_settings, created = ManagerPrivacySetting.objects.get_or_create(profile=manager_profile)
context['manager_form'] = ManagerPrivacySettingForm(prefix='manager', instance=manager_settings)
manager_activity_settings = []
for action in Action.objects.all().exclude(privacy_settings_text__isnull=True).exclude(privacy_settings_text=''):
setting, created = ActivityPrivacySetting.objects.get_or_create(profile_content_type=ContentType.objects.get_for_model(manager_profile), profile_object_id=manager_profile.id, action=action)
manager_activity_settings.append(setting)
context['manager_activity_settings'] = manager_activity_settings
context['PRIVACY_LEVEL_CHOICES'] = PRIVACY_LEVEL_CHOICES
context['user_info_form'] = user_info_form
context['email_form'] = email_settings_form
context['password_form'] = password_form
context['current'] = 'settings'
context['has_linked_in_profile'] = LinkedInProfile.objects.filter(user=profile.user).exists()
context['has_facebook_profile'] = FacebookProfile.objects.filter(user=profile.user).exists()
context['tag_types'] = TagType.objects.all()
return render_to_response('settings/base.html', context, context_instance=RequestContext(request))
@login_required
def email_settings(request):
settings = EmailEvent.objects.all()
setting_object = dict()
for setting in settings:
setting_object[setting], created = EmailSetting.objects.get_or_create(user=request.user, event=setting)
if request.method == 'POST':
email_settings_form = EmailSettingsForm(request.POST)
if email_settings_form.is_valid():
for setting in settings:
if email_settings_form.cleaned_data.get(setting.name) != setting_object.get(setting).enabled:
setting_object[setting].enabled = email_settings_form.cleaned_data.get(setting.name, False)
setting_object[setting].save()
messages.success(request, 'Email settings have been saved')
return HttpResponseRedirect('/account/settings/#settings-email')
else:
return HttpResponseRedirect('/account/settings/#settings-email')
forms.py
class EmailSettingsForm(forms.Form):
def __init__(self, *args, **kwargs):
super(EmailSettingsForm, self).__init__(*args, **kwargs)
settings = EmailEvent.objects.all()
for setting in settings:
self.fields[setting.name] = forms.BooleanField(required=False)
template.html
<form method="POST" action="/account/settings/email/">
<section class="main">{% csrf_token %}
<h4>Email me when</h4>
<p>{{ email_form.dormant_user }}
<label for="id_same_tag_added" class="checkbox-label">I haven't been to the site in a while.</label>
</p>
<p>{{ email_form.same_tag_added }}
<label for="id_same_tag_added" class="checkbox-label">Someone adds the same interests.</label>
</p>
<p>{{ email_form.message_received }}
<label for="id_message_received" class="checkbox-label">I receive a message.</label>
</p>
<p>{{ email_form.investment_verification }}
<label for="id_investment_verification" class="checkbox-label">I have new investments to verify.</label>
</p>
<p>{{ email_form.fund_introduction }}
<label for="id_fund_introduction" class="checkbox-label">I receive a new introduction.</label>
</p>
<p>{{ email_form.employment_verification }}
<label for="id_employment_verification" class="checkbox-label">I receive new employment requests.</label>
</p>
<p>{{ email_form.friend_request }}
<label for="id_friend_request" class="checkbox-label">I receive a new connection request.</label>
</p>
<p>{{ email_form.connection_accepted }}
<label for="id_friend_request" class="checkbox-label">I have a new connection accepted.</label>
</p>{% if role == 'manager' %}
<p>{{ email_form.management_verification }}
<label for="id_management_verification" class="checkbox-label">Email me when I have new management positions to verify.</label>
</p>{% endif %}
<p>{{ email_form.weekly_newsletter }}
<label for="id_weekly_newsletter" class="checkbox-label">Send me weekly newsletter.</label>
</p>
<input type="hidden" name="next" value="/account/settings/">
</section>
<section class="bottom">
<button type="submit" class="btn btn-success btn-large" name="basic"> <i class="icon-ok"></i> Save changes</button>
</section>
</form>
呈现模板
<form method="POST" action="/account/settings/email/">
<section class="main">
<div style='display:none'>
<input type='hidden' name='csrfmiddlewaretoken' value='####################' />
</div>
<h4>Email me when</h4>
<p>
<label for="id_same_tag_added" class="checkbox-label">I haven't been to the site in a while.</label>
</p>
<p>
<label for="id_same_tag_added" class="checkbox-label">Someone adds the same interests.</label>
</p>
<p>
<label for="id_message_received" class="checkbox-label">I receive a message.</label>
</p>
<p>
<label for="id_investment_verification" class="checkbox-label">I have new investments to verify.</label>
</p>
<p>
<label for="id_fund_introduction" class="checkbox-label">I receive a new introduction.</label>
</p>
<p>
<label for="id_employment_verification" class="checkbox-label">I receive new employment requests.</label>
</p>
<p>
<label for="id_friend_request" class="checkbox-label">I receive a new connection request.</label>
</p>
<p>
<label for="id_friend_request" class="checkbox-label">I have a new connection accepted.</label>
</p>
<p>
<label for="id_management_verification" class="checkbox-label">Email me when I have new management positions to verify.</label>
</p>
<p>
<label for="id_weekly_newsletter" class="checkbox-label">Send me weekly newsletter.</label>
</p>
<input type="hidden" name="next" value="/account/settings/">
</section>
<section class="bottom">
<button type="submit" class="btn btn-success btn-large" name="basic"> <i class="icon-ok"></i> Save changes</button>
</section>
</form>
渲染的模板没有来自{{email_form.fields}}的输入,任何人都可以告诉我们可能出错了吗?
答案 0 :(得分:1)
似乎您将“上下文”定义为常规python字典。你需要使用:
from django.template import Context
context = Context()