-1
我正在嘗試創建一個註冊表單,其中包含添加到配置文件模型的信息,但是當我提交時,它會一直給我一個匿名用戶。我在這裏做錯了什麼?Django註冊表單,匿名用戶?
我forms.py:
class RegisterForm(forms.ModelForm):
Gebruikersnaam = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Gebruikersnaam'}), max_length=20, label="")
Wachtwoord = forms.CharField(widget=forms.PasswordInput(attrs={'placeholder': 'Wachtwoord'}), label="")
Emailadres = forms.EmailField(required=False, widget=forms.TextInput(attrs={'placeholder': 'E-mailadres'}), label="")
Profielfoto = forms.ImageField(required=False, label="")
Biografie = forms.CharField(required=False, widget=forms.TextInput(attrs={'placeholder': 'Biografie'}), max_length=150, label="")
class Meta:
model = Profiel
fields = ['Gebruikersnaam', 'Wachtwoord', 'Emailadres', 'Profielfoto', 'Biografie']
我的models.py:
class Profiel(models.Model):
user = models.OneToOneField(User, primary_key=True)
Profielfoto = models.ImageField()
Biografie = models.CharField(max_length=150)
和我Views.py:
def step3(request):
if request.method == 'POST':
form = RegisterForm(request.POST)
if form.is_valid():
Registratieform = form.save(commit=False)
username = form.cleaned_data['Gebruikersnaam']
password = form.cleaned_data['Wachtwoord']
user = authenticate(username=username, password=password)
Registratieform.user = request.user # Set the user object here
Registratieform.save() # Now you can send it to DB
if Registratieform.user is not None:
if Registratieform.user.is_active:
login(request, Registratieform.user)
return redirect('/Dashboard/')
return render_to_response("Home/index.html", {'form': form})
else:
form = RegisterForm()
return render(request, 'Home/index.html', {'form': form})
用戶確實是登記前未認證。我應該如何使用提交的表單正確創建用戶?我還在學習,不能解決這個問題.. – Acework