找不到带有关键字参数'{'pk':''}'的'profile_settings'。尝试了1个模式:['users \\ / profile \\ / settings \\ /(?P <pk> [0-9] +)$']

时间:2019-01-21 17:08:52

标签: python django django-templates

我有CustomUserProfile模型。两个表都有一对一的关系。 我使用了post_save信号,因此,无论何时创建用户,都会自动创建一个空的配置文件。 个人资料仅具有updateView,因为它是在创建用户时已经创建的。

问题:每当我访问个人资料时,都会出现此错误:

  

未找到关键字参数为'{'pk':''}'的'profile_settings'反向。尝试了1个模式:['users \ / profile \ / settings \ /(?P [0-9] +)$']“ **

我肯定在模板上犯了一个错误。 在下面附加所有相关的代码段。

models.py

class CustomUser(AbstractUser):
    age = models.PositiveIntegerField(null=True, blank=True)

class Profile(models.Model):

    user            = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
    contact_mobile  = models.CharField(max_length=11, null=True, blank=True)
    shipping_id     = models.ForeignKey(Shipping, related_name='profile', on_delete=models.CASCADE, null=True, blank=True)
    biography = models.CharField(max_length=700),

def create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

post_save.connect(create_profile, sender=CustomUser)

views.py

class ProfileSettingsView(UpdateView):
    model = Profile
    form_class = ProfileSettingsForm
    pk_url_kwarg = 'pk'
    context_object_name = 'object'
    template_name = 'profile_settings.html'

    def get_object(self):
        pk = self.kwargs.get('pk')
        return get_object_or_404(Profile, id=pk)

    def get_context_data(self, **kwargs):
        c_object = self.get_object()
        context = super(ProfileSettingsView, self).get_context_data(**kwargs)
        context['user'] = c_object.user
        context['shipping_id'] = c_object.shipping_id
        context['contact_mobile'] = c_object.contact_mobile
        print('------------------------------------------------------')
        print('context: ', context)
        return context

class DashboardView(DetailView):
    model = CustomUser
    pk_url_kwarg = 'pk'
    context_object_name = 'object'
    template_name = 'dashboard.html'

urls.py


path('dashboard/', views.dashboard, name='dashboard'),
path('profile/settings/<int:pk>', views.ProfileSettingsView.as_view(), name='profile_settings'),

dashboard.html

<div class="col-lg-3">
    <nav class="account-bar">
        <ul>
            <li class="active"><a href="{% url 'users:dashboard' %}">Dashboard</a></li>
            <li><a href="{% url 'users:profile_settings' pk=object.id %}">Profile Settings</a></li>                          
        </ul>
    </nav>
</div>

0 个答案:

没有答案