TypeError: Field.__init__() got an unexpected keyword argument 'Label'

I am coding a blog for myself. I'm coding for a user to register an account, but I'm getting some errors as follows:

File "C:\Users\hitechlab\Desktop\mysite\home\urls.py", line 18, in from . import views File "C:\Users\hitechlab\Desktop\mysite\home\views.py", line 4, in from .forms import RegistrationForm File "C:\Users\hitechlab\Desktop\mysite\home\forms.py", line 9, in class RegistrationForm(forms.Form): File "C:\Users\hitechlab\Desktop\mysite\home\forms.py", line 10, in RegistrationForm username = forms.CharField(Label='Tài Khoản', max_length=30)

File "C:\Users\hitechlab\AppData\Local\Programs\Python\Python310\lib\site-packages\django\forms\fields.py", line 267, in init super().init(**kwargs) TypeError: Field.init() got an unexpected keyword argument 'Label'

-I can't find my error anywhere, hope someone can help. Here is my code

urls.py

from unicodedata import name
from django.urls import path

from blog.models import Post
from . import views
urlpatterns = [
    path('', views.list, name = 'blog'),
    path('<int:id>/', views.post, name = 'post'),
]
forms.py
import email

from tkinter import Label, Widget

from django import forms
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist
class RegistrationForm(forms.Form):
    username = forms.CharField(Label='Tài Khoản', max_length=30)
    email = forms.EmailField(Label='Email')
    password1=forms.CharField(Label= 'Mật Khẩu', Widget = forms.PasswordInput())
    password2=forms.CharField(Label= 'Nhập lại mật khẩu', Widget = forms.PasswordInput())

def clean_password2(self):
        if 'password1' in self.cleaned_data:
            password1= self.cleaned_data ['password1']
            password2= self.cleaned_data ['password2']
            if password1==password2 and password1:
                return password2 
            raise forms.ValidationError('Mật khẩu không hợp lệ')
    def clean_username (self):
        username= self.clean_data['username']
        if not re.search(r'^\w+$',username):
            raise forms.ValidationError("Tên tài khoản có kí tự đặc biệt")
        try:
            User.objects.get(username=username) 
        except ObjectDoesNotExist:
            return username
        raise forms.ValidationError('Tài Khoản không tồn tại')
    def save(self):
        User.objects.create_user(username=self.clean_data['username'], email=self.clean_data['email'], password= self.clean_data ['password1'])

Django's CharField class does not accept a Label input - if you remove those inputs from your CharField init calls, that should clear the error!

The name of the parameters is label=… [Django-doc] and widget=… [Django-doc], these are parameters of the function calls, you also do not need to use tkinter for anything, so:

# forms.py
import email

from django import forms
import re
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist


class RegistrationForm(forms.Form):
    username = forms.CharField(label='Tài Khoản', max_length=30)
    email = forms.EmailField(label='Email')
    password1 = forms.CharField(label='Mật Khẩu', widget=forms.PasswordInput())
    password2 = forms.CharField(
        label='Nhập lại mật khẩu', widget=forms.PasswordInput()
    )

    def clean_password2(self):
        if 'password1' in self.cleaned_data:
            password1 = self.cleaned_data['password1']
            password2 = self.cleaned_data['password2']
            if password1 == password2 and password1:
                return password2
            raise forms.ValidationError('Mật khẩu không hợp lệ')

    def clean_username(self):
        username = self.clean_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValidationError("Tên tài khoản có kí tự đặc biệt")
        try:
            User.objects.exclude(pk=self.instance.pk).get(username=username)
        except ObjectDoesNotExist:
            return username
        raise forms.ValidationError('Tài Khoản không tồn tại')

        def save(self):
            return User.objects.create_user(
                username=self.clean_data['username'],
                email=self.clean_data['email'],
                password=self.clean_data['password1'],
            )
Back to Top