运行
python manage.py makemigrations
命令时出现以下错误:
Traceback (most recent call last):
File "/home/edward/Desktop/Lynia-remittance-platform-/remittance_platform/manage.py", line 22, in <module>
main()
File "/home/edward/Desktop/Lynia-remittance-platform-/remittance_platform/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
utility.execute()
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/management/base.py", line 412, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/management/base.py", line 453, in execute
self.check()
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/management/base.py", line 485, in check
all_issues = checks.run_checks(
^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/checks/urls.py", line 14, in check_url_config
return check_resolver(resolver)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/core/checks/urls.py", line 24, in check_resolver
return check_method()
^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/urls/resolvers.py", line 494, in check
for pattern in self.url_patterns:
^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/urls/resolvers.py", line 715, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
^^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/utils/functional.py", line 57, in __get__
res = instance.__dict__[self.name] = self.func(instance)
^^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/urls/resolvers.py", line 708, in urlconf_module
return import_module(self.urlconf_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/edward/Desktop/Lynia-remittance-platform-/remittance_platform/remittance_platform/urls.py", line 23, in <module>
path('', include('remittance.urls')),
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/urls/conf.py", line 38, in include
urlconf_module = import_module(urlconf_module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.11/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 940, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/home/edward/Desktop/Lynia-remittance-platform-/remittance_platform/remittance/urls.py", line 3, in <module>
from . import views
File "/home/edward/Desktop/Lynia-remittance-platform-/remittance_platform/remittance/views.py", line 7, in <module>
from .forms import TransactionForm
File "/home/edward/Desktop/Lynia-remittance-platform-/remittance_platform/remittance/forms.py", line 40, in <module>
class RegisterForm(forms.ModelForm):
File "/home/edward/Desktop/Lynia-remittance-platform-/venv/lib/python3.11/site-packages/django/forms/models.py", line 321, in __new__
raise FieldError(message)
django.core.exceptions.FieldError: Unknown field(s) (password1, password2) specified for User
模型.py
from django.db import models
from django.contrib.auth.models import AbstractUser
class User(models.Model):
username = models.CharField(max_length=255)
email = models.EmailField(unique=True)
password = models.CharField(max_length=128)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
phone_number = models.CharField(max_length=10)
class RemittanceUser(AbstractUser):
password1 = models.CharField(max_length=128, verbose_name='password',
help_text='<ul><li>Your password must be at least 8 characters long.</li>'
'<li>Your password must contain at least one uppercase letter.</li>'
'<li>Your password must contain at least one lowercase letter.</li>'
'<li>Your password must contain at least one number.</li>'
'<li>Your password must contain at least one special character.</li></ul>')
password2 = models.CharField(max_length=128, verbose_name='password confirmation',
help_text='Enter the same password as above, for verification.')`
forms.py
from django import forms
from remittance.models import Transaction
from django.contrib.auth.models import User
from django.db import models
from django.forms import ModelForm
from django.contrib.auth import authenticate
from remittance.models import Transaction
class CreateTransactionForm(forms.Form):
sender = forms.CharField(max_length=255)
recipient = forms.CharField(max_length=255)
amount = forms.DecimalField(max_digits=10, decimal_places=2)
currency = forms.CharField(max_length=3)
class TransactionHistoryForm(ModelForm):
class Meta:
model = Transaction
fields = ['sender', 'recipient', 'amount', 'currency', 'status']
class TransactionStatusForm(ModelForm):
class Meta:
model = Transaction
fields = ['transaction_status']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['transaction_status'].choices = [
('initiated', 'Initiated'),
('in_progress', 'In Progress'),
('completed', 'Completed'),
('cancelled', 'Cancelled'),
]
class UpdateTransactionStatusForm(ModelForm):
class Meta:
model = Transaction
fields = ['status']
class LoginForm(forms.Form):
username = forms.CharField(max_length=255)
password = forms.CharField(max_length=128, widget=forms.PasswordInput)
def clean(self):
username = self.cleaned_data['username']
password = self.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is None:
raise forms.ValidationError('Invalid username or password.')
self.user = user
return self.cleaned_data
class RegisterForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email', 'new_password1', 'new_password2']
def clean_username(self):
username = self.cleaned_data['username']
if User.objects.filter(username=username).exists():
raise forms.ValidationError('Username already exists.')
return username
def clean_email(self):
email = self.cleaned_data['email']
if User.objects.filter(email=email).exists():
raise forms.ValidationError('Email address already exists.')
return email
def clean_password2(self):
new_password1 = self.cleaned_data['password1']
new_password2 = self.cleaned_data['password2']
if new_password1 != new_password2:
raise forms.ValidationError('Passwords do not match.')
return new_password2
class ForgotPasswordForm(forms.Form):
email = forms.EmailField(max_length=255)
def clean_email(self):
email = self.cleaned_data['email']
if not User.objects.filter(email=email).exists():
raise forms.ValidationError('Email address not found.')
return email
class ResetPasswordForm(forms.Form):
new_password1 = forms.CharField(max_length=128, widget=forms.PasswordInput)
new_password2 = forms.CharField(max_length=128, widget=forms.PasswordInput)
def clean_new_password2(self):
new_password1 = self.cleaned_data['new_password1']
new_password2 = self.cleaned_data['new_password2']
if new_password1 != new_password2:
raise forms.ValidationError('Passwords do not match.')
return new_password2
class TransactionForm(ModelForm):
class Meta:
model = Transaction
fields = ['sender', 'recipient', 'amount', 'currency', 'status']
views.py
from django.shortcuts import render
from remittance.models import Transaction
from django.contrib.auth.models import User
from django.forms import ModelForm
from django.contrib.auth import authenticate
from remittance.models import Transaction
from .forms import TransactionForm
# Create your views here.
def home(request):
transactions = Transaction.objects.all()
return render(request, 'remittance/home.html', {
'transactions': transactions,
})
def create_transaction(request):
if request.method == 'POST':
# Get the form data from the request
form = TransactionForm(request.POST)
# Validate the form data
if form.is_valid():
# Create a new transaction
transaction = form.save()
# Redirect the user to the home page
return redirect('home')
else:
# The form hasn't been submitted yet, so show it to the user
form = TransactionForm()
return render(request, 'remittance/create_transaction.html', {
'form': form,
})
def transaction_history(request, user_id):
user = User.objects.get(id=user_id)
transactions = user.transactions.all()
return render(request, 'remittance/transaction_history.html', {
'transactions': transactions,
})
def update_transaction_status(request, transaction_id):
transaction = Transaction.objects.get(id=transaction_id)
if request.method == 'POST':
# Get the form data from the request
form = TransactionStatusForm(request.POST, instance=transaction)
# Validate the form data
if form.is_valid():
# Update the status of the transaction
form.save()
# Redirect the user to the home page
return redirect('home')
else:
# The form hasn't been submitted yet, so show it to the user
form = TransactionStatusForm(instance=transaction)
return render(request, 'remittance/update_transaction_status.html', {
'form': form,
})
def login(request):
if request.method == 'POST':
# Get the username and password from the request
username = request.POST['username']
password = request.POST['password']
# Authenticate the user
user = authenticate(username=username, password=password)
# If the user is authenticated, log them in
if user is not None:
login(request, user)
return redirect('home')
else:
# The username or password is incorrect
error_message = 'Invalid username or password.'
return render(request, 'remittance/login.html', {
'error_message': error_message,
})
else:
# The user hasn't submitted the login form yet, so show it to them
return render(request, 'remittance/login.html')
def logout(request):
logout(request)
return redirect('home')
def register(request):
if request.method == 'POST':
# Get the form data from the request
form = UserCreationForm(request.POST)
# Validate the form data
if form.is_valid():
# Create a new user
user = form.save()
# Log the user in
login(request, user)
# Redirect the user to the home page
return redirect('home')
else:
# The form hasn't been submitted yet, so show it to the user
form = UserCreationForm()
return render(request, 'remittance/register.html', {
'form': form,
})
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('create-transaction/', views.create_transaction, name='create_transaction'),
path('transaction_history/', views.transaction_history, name='transaction_history'),
#path('transaction-history/<int:user_id>/', views.transaction_history, name='transaction_history'),
path('update-transaction-status/', views.update_transaction_status, name='update_transaction_status'),
#path('update-transaction-status/<int:transaction_id>/', views.update_transaction_status, name='update_transaction_status'),
path('login/', views.login, name='login'),
path('register/', views.register, name='register'),
]
这些是代码,它们返回错误: 当我运行 python manage.py makemigrations
命令时,为用户指定了未知字段(密码 1、密码 2)。我自定义了用户模型,并将其命名为
RemittanceUser
,继承自
AbstractUser
。在我的
RemittanceUser
模型中,我定义了
password1
和
password2
字段。然而,Django 似乎无法找到这些字段。
错误原因:
如回溯所示,您正在使用User
中的
RegisterForm
模型:
class RegisterForm(forms.ModelForm):
class Meta:
model = User # Here you are using the User model for the RegisterForm
fields = ['username', 'email', 'new_password1', 'new_password2']
但是,您的 User
模型没有字段
new_password1
和
new_password2
。相反,它有
username
、
email
、
password
、
first_name
、
last_name
和
phone_number
。
class User(models.Model):
username = models.CharField(max_length=255)
email = models.EmailField(unique=True)
password = models.CharField(max_length=128)
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
phone_number = models.CharField(max_length=10)
解决方案
默认的 User
模型已经几乎拥有您想要的一切。如果您只想向其中添加
phone_number
,一种方法是创建另一个类(配置文件),然后执行
OneToOneField
将其连接到默认的
User
模型,如此处所述,或者可能更好,使用您的自定义用户模型,并按照已给出的链接中显示的方式进行设置。
settings.py
文件中添加
AUTH_USER_MODEL
变量:
AUTH_USER_MODEL = 'app_name.UserModelName'
例如:
AUTH_USER_MODEL = 'account.User'
>>> python manage.py makemigrations
>>> python manage.py migrate
>>> python manage.py runserver
forms.py
文件中导入用户模型:
from django.contrib.auth import get_user_model
User = get_user_model()
class RegisterForm(forms.ModelForm):
class Meta:
model = User
fields = ['username', 'email', 'new_password1', 'new_password2']