English 中文(简体)
Django 1.4 创建Edit用户简介账户
原标题:Django 1.4 Creating an Edit User Profile Account Settings View

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).
Using this site: http://www.turnkeylinux.org/blog/django-profile 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)

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.

迄今为止,我已经(全心全意地所有相关部分)

file directory: ~/Documents/project1

settings.py

AUTH_PROFILE_MODULE = "accounts.Account"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~ ~~~~~

File location: project1/accounts

models.py

# 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

~ ~~~~~~~~~~~~~~~~~~~~~

views.py

# 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        

由于该守则目前是我所介绍的,试图通过现有账户进行查询,直到找到一个配对用户链接,然后将其保留到一个可变的“用户解决方案”。 我采取的办法是将这一变量作为用户解决方案。 性别、用户分布。 出生日期等进入每个用户环境,如果更新和确定以前的价值(如果在Django功能中做了一系列陈述或部分内容),则会发生变化。

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

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 https://bitbucket.org/filmaster/filmaster-test/src/1618b18003ed/film20/userprofile/views.py to handle this error but this also did not yield any positive results.

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.

Thanks in advance!

最佳回答

为了了解用户物体的简介,您仅需要打电话get_profile(<>/code>。

user.get_profile()

For example,

request.user.get_profile()

https://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users”rel=“nofollow”>。 值得注意的是:

The method get_profile() does not create a profile if one does not exist.

So you have to have a trigger to create profiles on creating users ...

问题回答

<代码>user.get_profile(>> is now depreciated, otherwise, Christine You have a OneTo One with theuser model, You can only use user.profile





相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签