【问题标题】:Django--- Allowing Users to only edit their profileDjango---允许用户只编辑他们的个人资料
【发布时间】:2017-11-10 15:47:44
【问题描述】:

我想允许用户只编辑他们的个人资料。这是我的网址:

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

现在,当用户点击“我的个人资料”时,他们将获得可以编辑的自己的个人资料,但如果他们在浏览器中手动编辑 urlpath 并输入其他用户的 ID,如下所示,他们可以查看和编辑其他用户的个人资料

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

这是我的看法

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')

现在在 user_form.html 中,我检查了用户是否经过身份验证,以便只有登录的用户可以查看个人资料页面,但仍然登录的用户可以查看其他用户的个人资料。

{% 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()

如何限制已登录用户只能编辑他们的个人资料?我知道在堆栈溢出中有很多类似的问题和可能重复的问题,但似乎对我的情况没有帮助。

提前致谢

【问题讨论】:

    标签: django authentication django-models django-forms django-views


    【解决方案1】:

    您可以像这样从您的网址中删除 pk

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

    然后只获取用户的个人资料

    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
    

    这样可以确保配置文件视图只加载用户自己的配置文件。

    另外,您可能希望将视图限制为仅允许登录用户。

    【讨论】:

    • @dan_kaufhold 但这不会用当前数据填充配置文件表单。我希望用它们的当前值填充字段
    • @user3612693 当您使用它时,模板将具有您可以为配置文件的字段引用的{{ object }} 变量和{{ form }} 变量以呈现包含您定义的所有字段的表单。然后它们将具有加载对象的初始值。
    猜你喜欢
    • 2015-10-08
    • 2023-04-10
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 2013-08-23
    相关资源
    最近更新 更多