Hello I am trying to create a register form but when I try to save the user to my database but the database is always empty even tough I get no errors and bugs. I believe I am saving the user to somewhere because when I try to create a user with the same information I get an error saying there is already a user with that username.
RegisterPage.html
<form method="post">
{% csrf_token %}
<div class="input-group" style="padding: 15px">
<input type="text" name="username" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
</div>
<div class="input-group" style="padding: 15px">
<input type="email" name="email" class="form-control" placeholder="E posta" aria-label="E posta" aria-describedby="basic-addon1">
</div>
<div class="input-group" style="padding: 15px">
<input type="password" name="password1" class="form-control" placeholder="Password" aria-label="Password" aria-describedby="basic-addon1">
</div>
<div class="input-group" style="padding: 15px">
<input type="password" name="password2" class="form-control" placeholder="Confirm Password" aria-label="Confirm Password" aria-describedby="basic-addon1">
</div>
<a href="http://localhost:8000">Zaten bir hesabın varmı?</a>
<div class="card-body">
<button type="submit" class="btn btn-primary">Kayıt Ol</button>
</div>
</form>
Forms\views.py
class UserRegistrationForm(forms.ModelForm):
password1 = forms.CharField(label='password1', widget=forms.PasswordInput)
password2 = forms.CharField(label='password2', widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password1', 'password2']
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 != password2:
self.add_error('password2', 'Şifreler uyuşmuyor')
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
Login\models.py
class User(models.Model):
userid = models.AutoField(db_column='UserId', primary_key=True) # Field name made lowercase.
username = models.CharField(db_column='UserName', max_length=20, db_collation='Turkish_CI_AS') # Field name made lowercase.
password = models.CharField(db_column='Password', max_length=20, db_collation='Turkish_CI_AS') # Field name made lowercase.
email = models.CharField(db_column='Email', max_length=20, db_collation='Turkish_CI_AS') # Field name made lowercase.
favourites = models.TextField(db_column='Favourites', blank=True, null=True) # Field name made lowercase. This field type is a guess.
recipes = models.TextField(db_column='Recipes', blank=True, null=True) # Field name made lowercase. This field type is a guess.
class Meta:
managed = False
db_table = 'User'
Login\views.py
def register(request):
if request.method == 'POST':
form = UserRegistrationForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Account created successfully')
return redirect('login')
else:
print(form.errors)
return render(request, 'RegisterPage.html', {'form': form, 'errors': form.errors})
else:
form = UserRegistrationForm()
return render(request, 'RegisterPage.html', {'form': form})
FoodRecipeApp\settings.py
DATABASES = {
"default": {
"ENGINE": "mssql",
"NAME": "FoodRecipeDB",
"USER": "",
"PASSWORD": "",
"HOST": "localhost",
"PORT": "1433",
"OPTIONS": {"driver": "ODBC Driver 17 for SQL Server",
},
},
}
If there is something I am missing please let me know.
Also this is the warning print(form.errors) gives me in the terminal if I try to register a username I already tried before. Clearly I am saving it to somewhere but where ?
- username
- A user with that username already exists.