简体   繁体   English

Django:使用slug而不是pk访问通用UpdateView

[英]Django: access generic UpdateView with slug instead of pk

The code for using the pk is the following: 使用pk的代码如下:

views.py views.py

class CreatorUpdate(LoginRequiredMixin, UpdateView):
    model = Creator
    fields = ['first_name', 'last_name', 'location', 'country']
    success_url = reverse_lazy('index')

    # these two methods only give access to the users own profile but not the others (prevents url hacking)
    def user_passes_test(self, request):
        if request.user.is_authenticated:
            self.object = self.get_object()
            return self.object.user == request.user
        return False

    def dispatch(self, request, *args, **kwargs):
        if not self.user_passes_test(request):
            return redirect_to_login(request.get_full_path())
        return super(CreatorUpdate, self).dispatch(request, *args, **kwargs)

urls.py urls.py

path('creator/<int:pk>/update/', views.CreatorUpdate.as_view(), name='creator-update')

HTML snippet to call the URL: 调用URL的HTML代码段:

<a href="{% url 'creator-update' user.pk %}">{{ user.get_username }}</a>

Now I would like to use a slug (username) instead of the pk to access the UpdateView. 现在,我想使用一个子弹(用户名)而不是pk来访问UpdateView。 I can successfully pass the username to the URL: 我可以成功将用户名传递给URL:

<a href="{% url 'creator-update' user.get_username %}">{{ user.get_username }}</a>

but seem not to be able to match it in urls.py 但似乎无法在urls.py中将其匹配

path('creator/<slug:slug>/update/', views.CreatorUpdate.as_view(), name='creator-update')

with the same logic. 具有相同的逻辑。 I also tried to set a SlugField in my Creator model like this: 我还尝试在我的Creator模型中设置SlugField,如下所示:

from django.template.defaultfilters import slugify
class Creator(models.Model):
    ...
    username = models.CharField(max_length=100)
    slug = models.SlugField()

    def save(self, *args, **kwargs):
        self.slug = slugify(self.username)
        super(Creator, self).save(*args, **kwargs)

    ...

And setting slug_field = 'slug' in the UpdateView also does not get it. 并且在UpdateView中设置slug_field = 'slug'也不会得到它。

These modification result in a 404 error: raised by catalog.views.CreatorUpdate: No creator found matching the query 这些修改导致404错误:由catalog.views.CreatorUpdate: No creator found matching the query引发catalog.views.CreatorUpdate: No creator found matching the query

When adjusting the HTML part to <a href="{% url 'creator-update' user.creator.slug %}">{{ user.get_username }}</a> I get a NoReverseMatch error Reverse for 'creator-update' with arguments '('',)' not found . 将HTML部分调整为<a href="{% url 'creator-update' user.creator.slug %}">{{ user.get_username }}</a>我收到了一个NoReverseMatch错误Reverse for 'creator-update' with arguments '('',)' not found NoReverseMatch Reverse for 'creator-update' with arguments '('',)' not found

What is the logic to use a slug to access a view? 使用子段访问视图的逻辑是什么?

The error is that the user object has no attribute slug. 错误是用户对象没有属性段。 The Creator model has the slug field. Creator模型具有“ slug”字段。 Create a ListView of Creator Model then check if each creator object has a slug. 创建创建者模型的ListView,然后检查每个创建者对象是否有一个块。

Correct models.py : 正确的models.py

class Creator(models.Model):
    ...
    slug = models.SlugField()

    def save(self, *args, **kwargs):
        self.slug = slugify(self.user)
        super(Creator, self).save(*args, **kwargs)

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

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