简体   繁体   English

为什么此代码不起作用,如何获取url的特定部分?

[英]Why this code doesn't work, how do I grab a specific part of url?

I have the following url http://localhost:8000/enthusiastically-synergize-optimal-results-after-proactive-total-linkage/edit/ and would like to access that slug from this url in this function so I can use it. 我有以下网址http://localhost:8000/enthusiastically-synergize-optimal-results-after-proactive-total-linkage/edit/并希望在此函数中从该网址访问该链接,以便使用。 In class based view I could use self.kwargs.get('smthng') and grab that but don't know how do I do that here: 在基于类的视图中,我可以使用self.kwargs.get('smthng')并抓住它,但不知道我该怎么做:

def permission_of_user_for_posts(request):
post = get_object_or_404(Blog, slug= request.GET.get('blog_slug'))
if request.user.username == post.author:
    return True
return False


@user_passes_test(permission_of_user_for_posts)
def blog_update(request, blog_slug):
blog = get_object_or_404(Blog, slug=blog_slug)
if request.method == 'POST':
    form = BlogForm(request.POST, request.FILES, instance=blog)
    if form.is_valid():
        form.instance.author = request.user
        form.save()
        return redirect('/')

form = BlogForm(instance=blog)
context= {
    'form': form
}
return render(request, 'blogs/form.html', context)

or is there something else that I am doing it wrong in permission_of_user_for_posts function? 还是在permission_of_user_for_posts函数中我做错了什么? please help me thank you 请帮我谢谢

edit: this is url 编辑:这是网址

urlpatterns = [
    path('', blogs_view.home, name='home'),
    path('blogs/', blogs_view.blogs, name='blogs'),
    path('<slug:blog_slug>', blogs_view.blog_detail, name='blog_detail'),
    path('post/', blogs_view.blog_create, name='blog_create'),
    path('<slug:blog_slug>/edit/', blogs_view.blog_update, name='blog_update'),
    path('<slug:blog_slug>/delete/', blogs_view.blog_delete, name='blog_delete'),
]

The way to do this is not to use the decorator or a separate function at all. 做到这一点的方法是根本不使用装饰器或单独的功能。 Just check the post in the view: 只需查看视图中的帖子即可:

def blog_update(request, blog_slug):
    blog = get_object_or_404(Blog, slug=blog_slug)
    if not request.user == blog.author:
        raise PermissionDenied 
    if request.method == 'POST':
        ...

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

相关问题 如何遍历数组的一部分,为什么我的循环不起作用? - How do I iterate through part of an array and why doesn't my loop work? PYTHON:如何抓取字符串的特定部分? - PYTHON: How do I grab a specific part of a string? 为什么请求在特定 URL 上不起作用? - Why Request doesn't work on a specific URL? 如何在 Python 中使用嵌套列表遍历列表/为什么我的代码不起作用? - How do I iterate through lists with nested lists in Python / why doesn't my code work? 为什么特定窗口的 PIL.ImageGrab.grab 无法正常工作? - Why does PIL.ImageGrab.grab of specific window doesn't work properly? 我的代码不起作用,我该如何解决? - My Code just doesn't work, how do I fix it? 为什么此代码不起作用以及如何修复它? - Why this code doesn't work and how to repair it? 如何在<span>带有特定 class 和特定文本的标签后面的 HTML 代码中获取下一行的字符串?</span> - How do I grab the string on the next line in HTML code following <span> tag with specific class and specific text? 从网址中获取产品代码,我是否需要正则表达式? - Grab product code from a url, do I need regex for this? 我正在尝试做一个点击游戏,但它不起作用,为什么? - I'm trying to do a clicker game but it doesn't work, why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM