![](/img/trans.png)
[英]changing the access: from everyone to restricted user, allowing only the user that created post to be able to edit and delete
[英]allowing a user to access adminpanel?
請幫助解決問題。
我創建了一個表格來注冊用戶。 注冊管理員面板后,我需要用戶具有訪問權限。 為此,我嘗試覆蓋字段is_staff的值:
class RegistrationForm(UserCreationForm):
is_staff = forms.CharField(
default=True,
widget=forms.HiddenInput(),
)
class Meta:
model = User
fields = (
'username',
'email',
'password1',
'password2',
'is_staff',
)
意見:
def registration(request):
"""
ajax refistration procedure
"""
result = False
form = RegistrationForm()
if request.method == 'POST' and request.is_ajax():
form = RegistrationForm(request.POST)
if form.is_valid():
try:
new_user = form.save()
except:
pass
else:
result = True
data = {
'result': result,
}
return HttpResponse(json.dumps(data), content_type='application/json')
問題是瀏覽器顯示以下錯誤消息:
TypeError at /
__init__() got an unexpected keyword argument 'default' Request Method: GET Request URL: Django Version: 1.6.8
Exception Type: TypeError Exception Value:
__init__() got an unexpected keyword argument 'default' Exception Location: /home/kalinins/.virtualenvs/kinopom_project/kinopom_env/local/lib/python2.7/site-packages/django/forms/fields.py
in __init__, line 198
您收到的錯誤意味着在創建is_staff
字段時關鍵字參數default=True
不正確。
您應該改用initial=True
來指定初始值 。
此外,您在is_staff
字段中使用了一個forms.CharField
,該字段可能應該是一個布爾值(所以是forms.BooleanField
)。 之后,它將顯示為一個復選框,而不是顯示為文本字符的字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.