简体   繁体   中英

Django User Profile - Forms

Django 1.4

Sorry if this is a silly question i am fairly new to Django. I am attempting to link a user and a profile together via the inbuilt auth profile system. All the examples of this i can find do not use a class based view, which is something i would really like to use.

Basically i would like a form that combines the Profile and the User allowing me to create both at the same time. If possible i would like to use the same form to Edit/Create the User + Profile.

I have created a model for the profile: Profile

Created forms:

class UserForm(forms.ModelForm):
    class Meta:
        model = User


class ProfileRegisterView(FormView):
    template_name = 'profile-register-form.html'
    form_class    = UserForm
    success_url   = '/account/created/'

Adding the profile to the user model does not seem to include it within the UserForm:

AUTH_PROFILE_MODULE = "creative_profile.Profile"

The 2nd alternative i have tried was to define individual forms in forms.py however the form_class attribute only accepts one form model..

Any pointers help would be great, thanks

I do it in more simple way (i suggest). Just use django build in. In urls.py I added (r'^login/$','django.contrib.auth.views.login') . In settings.py add LOGIN_URL='/login/' and to MIDDLEWARE_CLASSES add 'django.contrib.auth.middleware.AuthenticationMiddleware' . Copy registration/login.html template locally if you want to change it. After such manipulations you will have ability to login as user. Forgot, you also should import from django.contrib.auth.models User and Group.

One possible solution is to include the Profile fields in your UserForm and override the save() method to populate the Profile fields.

The save() method will have to include a get_or_create() call for the Profile model if you're not using a post_save signal to create it. If you are using a post_save signal to create the Profile model, you're going to have to make sure the User is being saved first before calling the get_profile() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM