我是Django 的初学者,我有一个学院项目.我通过向其中添加一些字段来扩展用户模型,并在允许用户登录的表单中使用它们.当我try 它时,它起作用了,但当我提交表单时,我收到以下错误: 无法将关键字‘UserName’解析到字段中.选项包括:ID、配置文件图像、状态、用户、用户ID
我知道这个问题是重复的,但我见过的所有 case 都与我的不同.
Models.py
from django.contrib.auth.models import User
from django.db import models
STATUS_CHOICES = (
('', 'What is your use of the academy?'),
('student', 'Student'),
('teacher', 'Teacher'),
('author', 'Author'),
)
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_image = models.ImageField(null=True, default='profile.jpg')
status = models.CharField(
max_length=150,
choices=STATUS_CHOICES,
default='What is your use of the academy?',
)
USERNAME_FIELD = 'user__username'
def __str__(self):
return self.user.username
Views.py
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import CreateView
from .forms import RegisterUserForm
# Create your views here.
def profile(request):
return render(
request,
'common/profile.html'
)
class RegisterView(CreateView):
form_class = RegisterUserForm
success_url = reverse_lazy('login')
template_name = 'registration/Register.html'
Urls.py
from django.urls import path, include
from django.contrib.auth.views import LoginView
from .forms import UserLoginForm
from . import views
urlpatterns = [
path('login/', LoginView.as_view(authentication_form=UserLoginForm), name='login'),
path('register/', views.RegisterView.as_view(), name='register'),
path('profile/', views.profile, name='profile'),
path('', include('django.contrib.auth.urls')),
]
Register.html
{% extends 'base.html' %}
{% load i18n %}
{% block content %}
<div class="container">
<div class="row justify-content-md-center">
<div class="card col-md-6 px-0">
<div class="card-header text-center">
<h3 class="mb-0 py-2">{% trans 'Signup' %}</h3>
</div>
<div class="card-body">
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{% include 'common/form.html' with form=form %}
<div class="d-grid gap-2 text-center">
<button type="submit" class="btn btn-primary d-grid gap-2 col-6 mx-auto">
{% translate 'Signup' %}
</button>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock content %}
这是我在GitHub上的回购: https://github.com/HamzaWaleedDV/e_academy个
我希望能得到你的帮助.
我try 了这个问题的解决方案:
How to Fix: Cannot resolve keyword 'user' into field. Choices are: description, end_time, id, start_time, title Django Error
Error: Cannot resolve keyword 'id' into field
Cannot resolve keyword 'username' into field. Choices are: city, id, phone, etc