2017-08-17 15 views
3

ユーザーがブートストラップグリフコンリンクをクリックすると別のページにリダイレクトする必要があるサイトがあります。このページは同じグリフコンで緑色で表示されるサイトですあたかもリンクを押すことによって、彼らはボタンを作動させた。この転覆中に私はProfileのフィールドactiveFalseからTrueへ行きたいと思っています。私は、次のコードを持っている:フォームを使用しないモデルのフィールドを設定する

models.py:

class Profile(models.Model): 
    user = models.OneToOneField(User, on_delete=models.CASCADE) 
    university = models.CharField(max_length=30, blank=True) 

    ROLE = (
     ('CUSTOMER', 'User'), # (value to be set on model, human readable value) 
     ('WORKER', 'Worker'), 
    ) 

    role = models.CharField(max_length = 20, choices = ROLE, default = 'USER') 


    active = models.BooleanField(default = False) 

views.py

def active(request): 
    request.user.profile.active = True; 
    return render(request, 'core/customer_active.html', {'user': request.user}) 

home.html:request.user.profile.active = True;がない理由

<a href="{% url 'active' %}"><span class="glyphicon glyphicon-ok-sign" aria-hidden="true"></span></href> 

私はわかりませんフィールドの状態を更新すると、何ができますか?

答えて

0

これは「アクティブ」プロパティの永続的な変更ですか?その場合は、ユーザーオブジェクトを保存する必要があります。

def active(request): 
    request.user.profile.active = True; 
    request.user.save() 
    return render(request, 'core/customer_active.html', {'user': request.user}) 

編集:この属性に彼らがこのビューをヒットするたびに保存することで、これはユーザーのプロファイルを更新するための賢い方法ではないことは注目に値するかもしれませんが、なぜあなたはちょうど迷っている場合は、このようなTrue値が持続していない、これが理由です。

2

他にも述べたように、保存する必要があります。ただし、のプロファイルは別のモデルであるため、ユーザーではなく保存する必要があります。

profile = request.user.profile 
profile.active = True 
profile.save() 
関連する問題