我已经查看了有关此错误的其他问题,但似乎无法查明我的问题。我知道有更好的方法来完成我在下面的代码中尝试做的一些事情,但是现在我只是想找出造成以下错误的罪魁祸首。
Internal Server Error: /users/create/
Traceback (most recent call last):
File "/home/ubuntu/workspace/venv_mymanagedservices/lib/python3.5/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/home/ubuntu/workspace/venv_mymanagedservices/lib/python3.5/site-packages/django/utils/deprecation.py", line 138, in __call__
response = self.process_response(request, response)
File "/home/ubuntu/workspace/venv_mymanagedservices/lib/python3.5/site-packages/django/middleware/clickjacking.py", line 32, in process_response
if response.get('X-Frame-Options') is not None:
AttributeError: 'str' object has no attribute 'get'
[26/Mar/2017 23:02:14] "POST /users/create/ HTTP/1.1" 500 72448
以下是我尝试创建新用户的视图:
class CustomerUserCreate(LoginRequiredMixin, CreateView):
model = User
template_name = 'users/user_create.html'
fields = ['username', 'name', 'email', 'password']
# Search for which customer the user is assigned to and deny access if not a customer user or superuser
def render_to_response(self, context):
customer_admins = Customer.objects.filter(users=self.request.user)
if not (customer_admins or self.request.user.is_superuser):
return redirect('/403')
else:
return super(CustomerUserCreate, self).render_to_response(context)
def get_form(self, form_class=None):
form = super(CustomerUserCreate,self).get_form(form_class) #instantiate using parent
customer_admins = Customer.objects.get(users=self.request.user)
# Add a field that allows user to select which companies should be added to the Company models' users field.
form.fields['companies'] = forms.models.ModelMultipleChoiceField(Company.objects.filter(customer=customer_admins))
form.fields['companies'].widget = forms.CheckboxSelectMultiple()
return form
def get_success_url(self):
return reverse('users:list')
def form_valid(self, form):
customer_admins = Customer.objects.filter(users=self.request.user)
if not (customer_admins or self.request.user.is_superuser):
return redirect('/403')
companies_data = form.cleaned_data['companies']
del form.cleaned_data['companies']
# Record which customer account this user should be associated with
form.instance.created_by_customer = Customer.objects.get(users=self.request.user)
new_user = User.objects.create_user(username=form.cleaned_data['username'], email=form.cleaned_data['email'], name=form.cleaned_data['name'], password=form.cleaned_data['password'])
if companies_data:
for company in companies_data:
company.users.add(new_user)
else:
messages.add_message(self.request, messages.WARNING, 'You must select at least 1 company for the new user.')
return reverse('users:create')
# Add a success message
messages.add_message(self.request, messages.SUCCESS, 'User Created.')
return reverse('users:list')
有没有人比我更清楚头脑?我想我可能会把自己与我想做的所有事情混为一谈,完全错过它。
答案 0 :(得分:0)
好的,正如您可能已经注意到的那样,我将新用户保存在form_valid中,因为我需要使用create_user函数,但之后我又返回了不再需要的表单。而不是返回表单,我返回new_user,这似乎工作。仍然不确定这是让客户在前端创建新用户的最佳方式,但似乎有效。
return super(CustomerUserCreate, self).form_valid(new_user)