用户注册时,它会同时创建一个用户个人资料。
account.user表中的列ID在配置文件表中具有FK,且created_by
Register / views.py
class RegisterFormView(TemplateView):
template_name = 'home/register.html'
def get(self, request, *args, **kwargs):
regis_form = RegisterForm()
return self.render_to_response({
'base_url' : settings.BASE_URL,
'regis': regis_form,
})
def post(self, request, *args, **kwargs):
if request.method == 'POST':
regis_form = RegisterForm(request.POST)
if regis_form.is_valid():
user = regis_form.save()
user.full_name = regis_form.cleaned_data.get("full_name")
user.email = regis_form.cleaned_data.get("email")
user.phone_number = regis_form.cleaned_data.get("phone_number")
user.set_password(regis_form.cleaned_data.get("password"))
password = regis_form.cleaned_data.get("password")
profile = Profile.objects.create(created_by=user)
user.save()
user = authenticate(phone_number=user.phone_number, password=password)
login(request, user)
messages.success(request, "Register success")
return redirect(reverse("home:register-success"))
else:
regis_form = RegisterForm(request.POST)
return self.render_to_response({
'base_url' : settings.BASE_URL,
'regis': regis_form,
})
个人资料/views.py
class ProfileView(TemplateView):
template_name = 'accounts/index.html'
def get(self, request, user_id):
data = get_object_or_404(Profile, pk=user_id)
return self.render_to_response({
'base_url' : settings.BASE_URL,
'data' : data,
})
个人资料/urls.py
url(r'^(?P<user_id>\d+)/$', ProfileView.as_view(), name='profile'),
Template / base.html
<a class="dropdown-item" href="{% url 'profile:profile' user.id %}">Profile</a>
注册并创建配置文件成功
但是我无法访问用户个人资料页面,我出现了错误
没有配置文件匹配给定的查询。
编辑:
Profile / models.py
class Profile(BaseModelUnique):
type = models.ManyToManyField(UserType, blank=True)
country = models.CharField(max_length=3, choices=constant.COUNTRY_CHOICES,
blank=True, null=True)
gender = models.PositiveIntegerField(choices=constant.GENDER_CHOICES,
default=3)
bio = models.TextField(blank=True, null=True)
background_cover = models.ImageField(
storage = storage.USER_COVER_STORAGE,
max_length=300,
blank = True,
null = True
)
avatar = models.ImageField(
storage = storage.USER_AVATAR_STORAGE,
max_length=300,
blank = True,
null = True
)
def __str__(self):
return self.owned_by.nick_name
User / models.py
class User(AbstractUser):
"""
Users within the Django authentication system are represented by this
model.
Username and password are required. Other fields are optional.
"""
def __str__(self):
return self.nick_name
def get_profile(self):
from core.structures.account.models import Profile
return Profile.objects.filter(created_by=self).first()
class Meta(AbstractUser.Meta):
swappable = 'AUTH_USER_MODEL'
unique_together = ('phone_number', 'nick_name')
class AbstractUser(AbstractBaseUser, PermissionsMixin):
id62 = models.CharField(max_length=100, db_index=True, blank=True, null=True)
phone_number = models.CharField(unique=True, max_length=20,
help_text=_('Required. 20 characters or fewer. digits only.'),
)
full_name = models.CharField(_('full name'), max_length=150, blank=True)
nick_name = models.CharField(_('nick name'), max_length=150, blank=True)
email = models.EmailField(_('email address'), blank=True)
is_staff = models.BooleanField(
_('staff status'),
default=False,
help_text=_('Designates whether the user can log into this admin site.'),
)
is_active = models.BooleanField(
_('active'),
default=True,
help_text=_(
'Designates whether this user should be treated as active. '
'Unselect this instead of deleting accounts.'
),
)
date_joined = models.DateTimeField(_('date joined'), default=timezone.now)
objects = UserManager()
EMAIL_FIELD = 'email'
USERNAME_FIELD = 'phone_number'
REQUIRED_FIELDS = ['email']
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
abstract = True
答案 0 :(得分:0)
我认为您在OnetoOneField
和User
模型中具有Profile
关系,因此您使用错误的user.id
访问个人资料。
使用{% url 'profile:profile' user.profile.id %}
个人资料ID ,而不是用户ID
<a class="dropdown-item" href="{% url 'profile:profile' user.profile.id %}">Profile</a>