0

I am trying to develop a profile edit page in Django 3.1. I am new to this so if something is funky just say so. I copied some of this from Django edit user profile

Views.py

from myapp.forms import UserForm, UserProfileInfoForm
from django.views.generic import (View, TemplateView, UpdateView)
from myapp.models import UserProfileInfo

class UpdateProfile(UpdateView):
    model = UserProfileInfoForm
    fields = ['first_name', 'last_name', 'email', 'phone', 'title', 'password']
    template_name = 'profile.html'
    slug_field = 'username'
    slug_url_kwarg = 'slug'

models.py

class UserProfileInfo(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    phone = PhoneField(blank=True, help_text='Contact phone number')
    title = models.CharField(max_length=255)
    system_no = models.CharField(max_length=9)

    def __str__(self):
       return self.user.username

forms.py

#...
from myapp.models import UserProfileInfo

class UserForm(forms.ModelForm):
    password = forms.CharField(widget=forms.PasswordInput())

    class Meta():
        model = User
        fields = ('username', 'email', 'password', 'first_name', 'last_name')

class UserProfileInfoForm(forms.ModelForm):
    class Meta():
        model = UserProfileInfo
        fields = ('phone', 'title', 'system_no')

myapp/urls.py

from django.urls import path
from . import views

app_name = 'myapp'

urlpatterns = [
    path('', views.index,name='index'),
    path('about/',views.AboutView.as_view(),name='about'),
    path('register/', views.register,name='register'),
    path('profile/', views.UpdateProfile.as_view(), name='profile'),
]

I have a link that only show after login in: base.html (href only)

<a class="nav-link active" href="{% url 'myapp:profile' %}">Profile</a>

Can you tell me what to fix? After clicking on the above link I get an error message in the browser

AttributeError at /profile/

type object 'UserProfileInfoForm' has no attribute '_default_manager'

Shane S
  • 1,747
  • 14
  • 31

1 Answers1

2

well you have done very good till here. Note these :

1.if you are using UpdateView class based view the model field must be the name of the model not the form.(UserProfileInfo)

2.if you are using class based view it is better for you to have a success url in class to redirect after successful attempt

3.if you are using UpdateView no need to have form in forms.py for that model. all you need is a form tag in your template update view will handle it for you and save the changes automatically.

  1. if you want to use form (if you want to save sth in instances that class with not automatically do it ) , better to use FormView and remember here you have to save the form by yourself link to FormView

If you want to use django for long time I strongly recommend to check this link and start reading the documents https://docs.djangoproject.com/en/3.1/ref/class-based-views/generic-editing/#django.views.generic.edit.FormView

  • forgot , the reason for your error is the model in class must be a mode not a form so change it and it will be ok , and your form is pretty useless here if you are using UpdateView – Roohollah Mozaffari Jan 29 '21 at 07:43