0

ユーザに自分のプロファイルのみを編集させたい。これは私のURLです:Django ---ユーザにプロファイルの編集のみを許可する

url(r'^profile/(?P<pk>[0-9]+)/$', views.UserUpdate.as_view(), name='profile') 

今「私のプロフィール」に関するユーザーのクリックが、彼らは編集することができますが、それらは手動でブラウザにURLパスを編集し、以下のように他のユーザーのIDを入力すると、自分のプロフィールを取得するとき、彼らは

http://127.0.0.1:8000/profile/1/ 

他のユーザーのプロフィールを表示して編集することができ、これは

ユーザーが認証されている場合のみ、ログインしているユーザーを争うことができるように私がチェックしたuser_form.htmlで今
class UserUpdate(UpdateView): 
model = Profile 
fields = ['personal_info','job_title','department', 'location','expertise', 'user_photo','phone_number','contact_facebook','contact_linkedin','contact_skype'] 
template_name = 'user_form.html' 
success_url = reverse_lazy('index') 

私の見解でありますプロファイルページは、まだログインしているユーザーは、他のユーザーのプロファイルを表示できます。

{% if user.is_authenticated %} 
        <h3> {{ user.first_name }}'s Profile</h3> 
        <form class="form-horizontal" action="" method="post" enctype="multipart/form-data"> 
        {% csrf_token %} 
        {% include 'form-template.html' %} 
        <div class="form-group"> 
         <div class="col-sm-offset-2 col-sm-10"> 
          <button type = "submit" class="btn btn-success">Submit</button> 
          <a href={% url 'index' %}><input type="button" class = " col-sm-offset-2 btn btn-warning " name="cancel" value="Cancel" /></a> 
         </div> 
        </div> 
        </form> 

これは私のモデルである:

class Profile(models.Model): 
user = models.OneToOneField(User, on_delete=models.CASCADE) 
personal_info = models.TextField(blank=True) 
job_title = models.CharField(max_length=100, blank=True) 
department = models.CharField(max_length=100, blank=True) 
location = models.CharField(max_length=100, blank=True) 
expertise = models.TextField(blank=True) 
phone_regex = RegexValidator(regex=r'^\+?1?\d{5,15}$', message="Phone number must be entered in the format: '+123456'. Between 5 and 15 digits allowed.") 
phone_number = models.CharField(validators=[phone_regex], max_length=16, blank=True) 
contact_skype = models.URLField(null=True, blank=True) 
contact_facebook = models.URLField(null=True, blank=True) 
contact_linkedin = models.URLField(null=True, blank=True) 
user_photo = models.ImageField(upload_to='../media/img', blank=True) 

@receiver(post_save, sender=User) 
def create_user_profile(sender, instance, created, **kwargs): 
    if created: 
     Profile.objects.create(user=instance) 
    instance.profile.save() 

@receiver(post_save, sender=User) 
def save_user_profile(sender, instance, **kwargs): 
    instance.profile.save() 

私は自分のプロファイルを編集するためにログインしているユーザを制限するにはどうすればよいですか?スタックオーバーフローに似た疑問や重複がたくさんあることは知っていますが、私の場合は役に立たないようです。あなただけのことを、あなたは確実にその方法をとても

url(r'^profile/$', views.UserUpdate.as_view(), name='profile') 

のようなあなたのURLからPKを外し、唯一のユーザーのプロファイルをフェッチ

class UserUpdate(UpdateView): 
    model = Profile 
    fields = ['personal_info','job_title','department', 'location','expertise', 'user_photo','phone_number','contact_facebook','contact_linkedin','contact_skype'] 
    template_name = 'user_form.html' 
    success_url = reverse_lazy('index') 

    def get_object(self): 
     return self.request.user.profile 

ことができ、事前

答えて

1

感謝プロファイルビューには、ユーザー自身のプロファイルのみが読み込まれます。

さらに、ログインしたユーザーのみを許可するように表示を制限したい場合があります。

+0

@dan_kaufholdしかし、これは現在のデータでプロフィールフォームを埋めることはありません。フィールドに現在の値でいっぱいにしてほしい – Uttam

+1

@Alasdairありがとう!答えを更新しました。 –

+0

@ user3612693これを使用すると、テンプレートはプロファイルのフィールドと '{{form}}'変数で参照できる '{{object}}'変数を持ち、定義したすべてのフィールドを含むフォームを表示します。次に、ロードされたオブジェクトの初期値が設定されます。 –

関連する問題