简体   繁体   English

如何添加链接到个人资料? 的Django 2.1.5

[英]How add link to profile? Django 2.1.5

I have url, but it work with error. 我有网址,但是它有错误。 For example, link must be example/profile/3, but template redirects to example/profile/4. 例如,链接必须是example / profile / 3,但是模板重定向到example / profile / 4。 Profile create with signals. 使用信号创建配置文件。 I watch, id user and id profile not matching.Maybe, error in this. 我看到ID用户名和ID个人资料不匹配。也许是错误的。 Template redirects to link with id "User", but not id "Profile"! 模板重定向到ID为“ User”的链接,而不是ID为“ Profile”的链接! Pls help me. 请帮助我。

models.py models.py

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    first_name = models.CharField(max_length=30, blank=True, default='')
    last_name = models.CharField(max_length=30, blank=True, default='')
    location = models.CharField(max_length=30, blank=True, default='')
    #other fields

    def get_absolute_url(self):
        return reverse('profile-detail', args=[str(self.id)])

    def __str__(self):
        return self.user.username


class Post(models.Model):
    user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    #other fields    

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('post-detail', kwargs={'pk': self.pk})

signals.py signals.py

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

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

admin.py admin.py

class ProfileAdmin(admin.ModelAdmin):
    list_display = ('user', 'first_name', 'last_name', 'location', 'about_me', 'contacts')

admin.site.register(Profile, ProfileAdmin)

views.py views.py

class PostCreateView(FormView):
    form_class = PostForm
    template_name = 'post/post_form.html'
    success_url = reverse_lazy('posts')

    def form_valid(self, form):
        response = super(PostCreateView, self).form_valid(form)
        form.instance.user = self.request.user
        form.save()
        return response

class PostDetailView(generic.DetailView):
    model = Post

current urls.py 当前的urls.py

url(r'^profile/(?P<pk>\d+)$', views.ProfileDetailView.as_view(), name='profile-detail'),

post_detail.html post_detail.html

{% extends "base_generic.html" %}
{% block content %}
<p><strong>Author:</strong> <a href="{% url 'profile-detail' post.user.pk %}">{{ post.user }}</a></p>
<p><strong>Description:</strong></p>
<p>{{ post.body|safe }}</p>
...

I think there are few changes you need do, like: 我认为您需要做的更改很少,例如:

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

You don't need this signal. 您不需要此信号。 As User and Profile is connected by OneToOne already, and they are separate instances. 由于User和Profile已通过OneToOne连接,因此它们是单独的实例。 If you update user , you don't need to update profile as well. 如果您更新user ,则也不需要更新profile

Also, the issue with your question is that you are redirected to wrong profile. 另外,您的问题是您被重定向到错误的配置文件。 I think the problem is you are passing wrong Primary Key via url here. 我认为问题是您在此处通过url传递了错误的主键。 You are looking for profile but you are passing user id. 您正在寻找个人资料,但您正在传递用户ID。 So you need to change like this: 因此,您需要像这样进行更改:

<p><strong>Author:</strong> <a href="{% url 'profile-detail' post.user.profile.pk %}">{{ post.user }}</a></p>

You should be able to access profile from user.profile . 您应该能够从user.profile访问profile For more details please check docs regarding OneToOne field. 有关更多详细信息,请检查有关OneToOne字段的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM