我有一个django应用程序,它扩展了用户模型,以包含更多收集有关新用户数据的字段。到目前为止,我已经设法使表单正常工作并正确地在模板上呈现字段。
我也尝试将新用户添加到名为agents
的组中,我已经以某种方式实现了这一目标,但在将新用户添加到agents
用户组时,我遇到了问题我想要显示存储在数据库中的所有新用户信息的模板。通常的django用户字段(例如email, name
)是可访问的,但不会显示我创建的其他表单字段。以下是我的代码:
forms.py
class MyRegistrationForm(UserCreationForm):
COUNTIES = (('Baringo','Baringo'),
('Bomet','Bomet'),
('Bungoma', 'Bungoma'),
)
first_name = forms.CharField(required = False)
last_name = forms.CharField(required = False)
photo = forms.ImageField()
email = forms.EmailField(required = True)
phone = forms.CharField(max_length=30)
company = forms.CharField(max_length=150)
county = forms.ChoiceField(choices=COUNTIES)
city = forms.CharField(required = True)
class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
def clean_email(self):
# Get the email
email = self.cleaned_data.get('email')
# Check to see if any users already exist with this email as a username.
try:
match = User.objects.get(email=email)
except User.DoesNotExist:
# Unable to find a user, this is fine
return email
# A user was found with this as a username, raise an error.
raise forms.ValidationError('This email address is already in use.')
def save(self,commit = True):
user = super(MyRegistrationForm, self).save(commit = False)
user.email = self.cleaned_data['email']
user.first_name = self.cleaned_data['first_name']
user.last_name = self.cleaned_data['last_name']
user.photo = self.cleaned_data['photo']
user.phone = self.cleaned_data['phone']
user.company = self.cleaned_data['company']
user.county = self.cleaned_data['county']
user.city = self.cleaned_data['city']
if commit:
user.save()
return user
views.py
def register_user(request):
if request.method == 'POST':
user_form = MyRegistrationForm(request.POST, request.FILES) # create form object
if user_form.is_valid():
# Create a new user object but avoid saving it yet
new_user = user_form.save(commit=False)
# Set the chosen password
new_user.set_password(user_form.cleaned_data['password2'])
# Save the User object
new_user.save()
my_group = Group.objects.get(name='agents')
my_group.user_set.add(new_user)
return render(request, 'administration/register.html', {'user_form':user_form})
else:
user_form = MyRegistrationForm()
return render(request,'administration/register.html',{'user_form':user_form})
模板
{% for agent in agents %}
<tr>
<td>
{% thumbnail agent.photo "50x50" crop="center" as im %}
<img src="{{ im.url }}" width="{{ im.width }}" height="{{ im.height }}" class="img-responsive table-img"> {% empty %}
<img src="{% static 'dashboard/assets/img/default-avatar.png' %}" class="img-responsive table-img" alt=""> {% endthumbnail %}
</td>
<td>{{ agent.first_name }} {{ agent.last_name }}</td>
<td>{{ agent.company }} </td>
<td>{{ agent.phone }}</td>
<td>{{ agent.email }}</td>
<td class="td-actions text-right">
<button type="button" data-toggle="tooltip" title="Edit Task" class="btn btn-info btn-simple btn-xs">
<i class="fa fa-edit"></i>
</button>
<button type="button" data-toggle="tooltip" title="Delete Task" class="btn btn-danger btn-simple btn-xs">
<i class="fa fa-times"></i>
</button>
</td>
</tr>
{% endfor %}