Я чувствую себя застрявшим, разделяя пользовательскую модель и модель пользовательского профиля, чтобы создать два отдельных представления и приложения в целом.Это работало для пользователя, но не для userprofile, я использовал userchangeform с user и forms.modelforms с UserProfile.Я пытаюсь использовать updateview, чтобы пользователь мог видеть и обновлять свою информацию.коды:
#user profile models file
from django.db import models
from easyinstall import settings
class UserProfile(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
picture = models.ImageField(upload_to='uploads/profile_images', blank=True)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
website = models.URLField(blank=True)
timestamp = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = 'users profiles'
def __str__(self):
return self.user.username
формы userprofile
from django import forms
from .models import UserProfile
class EditProfileForm(forms.ModelForm):
class Meta:
model = UserProfile
fields = ('picture', 'bio', 'location', 'birth_date', 'website',)
представления userprofile
from .models import UserProfile
from django.views.generic import UpdateView
from django.urls import reverse_lazy
from .forms import EditProfileForm
class ProfileUpdateView(UpdateView):
model = UserProfile
form_class = EditProfileForm
template_name = 'profiles/profileupdate.html'
success_urls = reverse_lazy('profiles')
pk_url_kwarg = 'UserProfile_pk'
context_object_name = 'UserProfile'
Я действительно застрял с этой штукой "pk", как получить pk дляпрофиль пользователя и использовать его?нормально ли иметь id и user_id в таблице профиля?
любезно, просмотрите шаблон:
{% load socialaccount %}
<h1>Django Allauth Tutorial</h1>
{% if user.is_authenticated %}
<p>Welcome {{ user.username }} !!!</p>
<li><a href="{% url 'settings' %}">change settings</a></li>
<li><a href="{% url 'profile_update' UserProfile_pk=user.pk %}">Edit Profile</a></li>
{% else %}
{% csrf_token %}
<ul>
<li><a href="{% provider_login_url 'linkedin' %}">Sign Up</a></li>
</ul>
{% endif %}
и URL:
from django.urls import path
from .views import UsersList, ProfileUpdateView
urlpatterns = [
path('', UsersList.as_view(), name='profiles'),
path('profile/(?P<UserProfile_pk>\d+)/edit/', ProfileUpdateView.as_view(), name='profile_update'),
]
Я используюdjango 2.1, python3.
спасибо