简体   繁体   English

我能创建一个无需登录管理面板即可添加新的django.contrib.auth用户的表单吗?

[英]Am I able to create a form that is able to add a new django.contrib.auth User without logging in to the admin panel?

Have created a form but unsure if is right and also unable to add a user, it will show TypeError/ This is how the form I want it to look like 已经创建了一个表单,但是不确定是否正确并且也无法添加用户,它将显示TypeError / 这是我希望它看起来像的样子

The following is my coding: 以下是我的编码:

 class Form_Add_User(forms.Form): name=forms.CharField(label="Name", max_length=50) dateofbirth=forms.DateField(label="Date of Birth", widget=forms.widgets.DateInput(format="%m/%d/%Y")) contactnum=forms.CharField(label="Contact Number", max_length=9) def adduser(request): if len(request.POST)>0: form=Form_Add_User(request.POST) if(form.is_valid()): name=form.cleaned_data['name'] dateofbirth=form.cleaned_data['dateofbirth'] contactnum=form.cleaned_data['contactnum'] new_user=User(name=name,dateofbirth=dateofbirth,contactnum=contactnum) new_user.save() return redirect('/adduser') else: return render(request,'adduser.html',{'form':form}) else: form=Form_Add_User return render(request,'adduser.html',{'form':form}) 

First off: it's always very useful to also post a full error message if you have one. 首先,如果有的话,也发布完整的错误消息总是非常有用的。 The more info you give us, the easier (and quicker!) is answering your question. 您提供给我们的信息越多,越容易(更快)回答您的问题。

I assume, your User model is not actually django's auth User model (see, if you had posted the model, I wouldn't have to guess). 我认为,您的User模型实际上不是django的auth User模型(请参阅,如果您已发布模型,则不必猜测)。 The form you pass to your template was not instantiated: 您传递给模板的表单未实例化:

#...
else:
    form=Form_Add_User() #!!
    return render(request,'adduser.html',{'form':form})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM