问题
I am building a site which uses userena for the profile and registration part. The problem is that I am trying to remove the mugshot upload part and the profile privacy(registered,open,closed) from edit profile page so that userena uses gravatar only and the profiles are public for all. But in the template there is just
<fieldset>
<legend>{% trans "Edit Profile" %}</legend>
{{ form.as_p }}
</fieldset>
<input type="submit" value="{% trans "Save changes" %}" />
</form>
I am trying to find out how to edit this or the views to remove the mugshot and privacy from the form but without success. Please help?
回答1:
Instead of editing userena forms directly you should sub it in your own forms.py file (accounts/forms.py for example) as mentioned in the FAQ and put the url above the userena include. Here is an example where I use crispy-forms to sub class the edit profile form for nice bootstrap forms:
accounts/forms.py
class EditProfileFormExtra(EditProfileForm):
class Meta:
model = get_profile_model()
exclude = ['user', 'mugshot', 'privacy', 'my_custom_field']
def __init__(self, *args, **kwargs):
super(EditProfileFormExtra, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_id = 'edit-profile-form'
self.helper.form_class = 'form-horizontal'
self.helper.form_method = 'post'
self.helper.help_text_inline = True
self.helper.add_input(Submit('submit', _('Save'), css_class='green'))
self.helper.layout = Layout(
Field('first_name', placeholder=_("First Name")),
Field('last_name', placeholder=_("Last Name")),
Field('language', css_class="chosen"),
Field('timezone', css_class="chosen"),
)
accounts/urls.py
urlpatterns = patterns(
'',
url(r'^signup/$', 'userena.views.signup', {'signup_form': SignupFormExtra}, name='signup'),
url(r'^signin/$', 'userena.views.signin', {'auth_form': SigninFormExtra}, name='signin'),
url(r'^(?P<username>[\.\w-]+)/edit/$', 'userena.views.profile_edit', {'edit_profile_form': EditProfileFormExtra}, name='edit-profile'),
url(r'^', include('userena.urls')),
)
You can do this with just about any form as you can see in the urls above. Basically it says at this url, use the original modules view, but replace the form argument with my own form.
回答2:
The best is to remove those two fields by editing the form itself. In the view.py of the userena package, simply change the EditProfileForm by adding 'mugshot' and 'privacy' to the exclude list:
class EditProfileForm(forms.ModelForm):
...
class Meta:
model = get_profile_model()
exclude = ['user', 'mugshot', 'privacy']
If you really want to change the template only, you can iterate through the form instead of using form.as_p. In this case you will have to add the markup for other field parameters (like labels, errors, non-field errors etc) - see an example a here.
{% for field in form %}
{% if field.name != 'mugshot' %}
{{ field }}
{% endif %}
{% endfor %}
来源:https://stackoverflow.com/questions/15984129/django-userena-removing-mugshot