Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango 1.4 Creating an Edit User Profile Account Settings View
    primarykey
    data
    text
    <p>Some background, I've been designing a user profile for our Django database system and am currently trying to create a views page to allow the user to edit their account settings (outside of the admin page). I have searched numerous sources, most of which recommend using .get_profile(), but this has not worked in my program no matter what approach I try (to my knowledge and research done). So far I have used the django main pages for .get_profile() to see how to properly use .get_profile() and found the AUTH_PROFILE_MODULE setting ( I will specify my settings in a bit).<br> Using this site: <a href="http://www.turnkeylinux.org/blog/django-profile" rel="nofollow">http://www.turnkeylinux.org/blog/django-profile</a> I found out some other methods of using .get_profile() but still nothing has worked. (I won't add the rest of the links i've tried as I could go on for a while)</p> <p>Back to the question, can anyone recommend a method that would work so that I can obtain the users information to set up some default values to maintain their current settings if they choose not to edit them and only update the ones with new submitted values.</p> <p>So far I have: (hopefully all the relevant parts)</p> <p>file directory: ~/Documents/project1</p> <p>settings.py</p> <pre><code>AUTH_PROFILE_MODULE = "accounts.Account" </code></pre> <p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`</p> <p>File location: project1/accounts</p> <p>models.py</p> <pre><code># additional model to incorporate our custom fields to the auth user model class Account(models.Model): userLink = models.OneToOneField(User) #link (pointer) to the users other information in User model birthdate = models.DateField(blank = True) # True makes this field optional gender = models.CharField(max_length = 1, choices = GENDER_CHOICE, null = True) def __unicode__(self): # define a unicode for the user to access return u'%s %s' % (self.userLink.first_name, self.userLink.last_name) # return first and last name # custom Form to change user account information (can't be a modelForm) class EditAccountForm(forms.Form): gender = forms.CharField(max_length = 1)#, choices = GENDER_CHOICE, null = True) birthdate = forms.DateField(widget = SelectDateWidget()) # True makes this field optional </code></pre> <p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p> <p>views.py</p> <pre><code># Account settings view @login_required def AccountSettings(request): sluggedSettingError = request.GET.get('error', '') # error message with slugged character settingError = sluggedSettingError.replace('-', ' ') settingForm = AdditionalForm(request.POST or None) # variable is called in edit_user.html # might want to have some way to create a new profile if the account doesn't exist userSettings = Account.objects.filter(userLink=request.user) # userSettings = request.user.get_profile() # returns the current settings of the users profile #### Currently trying to get it to let me use get_profile() ######## print "userLink = %s" % userSettings#.user # print "Gender = %s" % userSettings.gender # testing print if request.method == 'POST': # if settingForm.is_valid(): # settingForm.save(); return HttpResponseRedirect('/') return render_to_response("user_settings.html", {'settingForm': settingForm, 'settingError': settingError}, context_instance = RequestContext(request)) # gender = request.POST['gender'] # pass in the gender variable from post # birthdate = request.POST['birthdate'] # pass in the birthdate from post </code></pre> <p>As the code is currently I gave up on the .get_profile() approach and tried doing a query through the available accounts until a matching userLink was found and then saving that to a variable "userSettings". The approach I was taking was to use this variable as userSettings.gender, userSettings.birthdate etc. to access each of the users settings and have them changed if updated and set to the previous value if not (with a series of if statements or some built in Django function).</p> <p>I'm not sure if this is the most efficient method, or if I should revert back to the get_profile() method. When I was using get_profile() (as it is typed currently in the commented out line) I got the following error Cannot resolve keyword 'user' into field. Choices are: birthdate, gender, id, userLink</p> <p>I also tried this approach with a new database using userLink defined as "user" instead to match what the django.docs website specified but still got an error about the Account not existing. I tried creating a try: and except: approach from <a href="https://bitbucket.org/filmaster/filmaster-test/src/1618b18003ed/film20/userprofile/views.py" rel="nofollow">https://bitbucket.org/filmaster/filmaster-test/src/1618b18003ed/film20/userprofile/views.py</a> to handle this error but this also did not yield any positive results.</p> <p>I am out of ideas on how to get .get_profile() to work, and can't think of anything to allow me to access the users stored information so that it can be edited in a form by them. I'm not sure I understand how to utilize it properly at this point and feel like I'm just running around in circles at this point. If anyone can help me with this I'd appreciate it. I tried to give as much relevant information as I could, but if you need something else to help me out let me know and I will try to include it as soon as I can.</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload