我试图在Django中搜索(默认)auth_user表的用户名。 但是我收到了一个错误。 我可以像在SQL“SELECT username FROM auth_user”中那样从表中获取/访问数据吗?
(Pdb) n
> /views.py(43)home()
-> if form.is_valid():
(Pdb) n
> /views.py(54)home()
-> errors = form.errors
(Pdb) n
> /views.py(55)home()
-> searched_user = ""
(Pdb) print(errors)
<ul class="errorlist"><li>username<ul class="errorlist"><li>同じユーザー名が既に登録済みです。</li></ul></li></ul>
“同じユーザー名が既に登录済みです”的意思是“相同的用户名已经注册”。
views.py:
@login_required(login_url='/')
def home(request):
from myapp.forms import UserSearch
formset = UserSearch
if request.method == 'POST':
form = formset(request.POST)
if form.is_valid():
from myapp.models import User
try:
username = form.cleaned_data['username']
searched_user = User.objects.filter(username=username)
except ObjectDoesNotExist:
errors = form.errors
searched_user = ""
return redirect('/home/')
else:
errors = form.errors
searched_user = ""
forms.py:
from django.contrib.auth.models import User
class UserSearch(forms.ModelForm): # ModelForm
class Meta:
model = User
fields = ('username',)
widgets = {
'username': forms.TextInput(attrs={'class': 'char', 'placeholder': 'username'}),
}
python:3.5.1,Django 1.9.2,python-social-auth:0.2.14