简体   繁体   English

URL传递我的Django项目中的参数

[英]URL passes parameter in my Django project

I am just a beginner of Django, these days I followed a mooc to learn Django, I wanted to set up my first website, but something went wrong and I can not figure out. 我只是Django的初学者,这些天我跟随一个mooc来学习Django,我想建立我的第一个网站,但出了点问题,我想不通。 I wanted to write a regex with parameter 'cate' in URLS.py to match the video function in my view.py, judging whether 'cate' eequals 'editors', if yes it will bring back data with attribute "editors_choice". 我想在URLS.py中编写一个带参数'cate'的正则表达式来匹配我的view.py中的视频函数,判断'cate'eequals'editor'是否正确,如果是,它将带回属性为“editors_choice”的数据。 However, I found no it never changes, so I printed 'cate' in view.py and found it always None and I still don't know why. 但是,我发现它永远不会改变,所以我在view.py中打印'cate'并发现它总是无,我仍然不知道为什么。

Following is my code: 以下是我的代码:

def video(request, cate=None):
    print(cate)
    context = {} =
    if cate is None:
        video_list = Video.objects.all()
    if cate == 'editors':
        video_list = Video.objects.filter(editors_choices=True)
    else:
        video_list = Video.objects.all()
    page_robot = Paginator(video_list, 16)
    page_num = request.GET.get('page')
    try:
        video_list = page_robot.page(page_num)
    except EmptyPage:
        video_list = page_robot.page(page_robot.num_pages)  # raise HTTP404("Empty")
    except PageNotAnInteger:
        video_list = page_robot.page(1)
    context['video_list'] = video_list
    return render(request, 'ten_movie.html', context)

urls.py中的'cate'参数


templete

Add a regex end-of-string character to the first pattern to prevent it from overlapping with the second. 将正则表达式字符串结尾字符添加到第一个模式以防止它与第二个模式重叠。

url(r'^video/$', video, name='video'),
url(r'^video/(?P<cate>[A-Za-z]+)$', video, name='video'),

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

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