url redirect with parameters

There are many ways to redirect django, you can Baidu by yourself. Here I recommend the way of redirect and reverse. It is very simple and can also solve the problem of passing parameters

An example is as follows:


#投票结果查看

def results(request,question_id=1):
    return HttpResponse(r"you're looking  at the results of question %s." % question_id )





#投票功能
def vote(request,question_id):
        '''
        投票逻辑
        '''
        #投票完后跳转到结果页面,如下
        return redirect(reverse('polls:results', args=[question_id]))



Description: We can see the voting function. After the user clicks to vote, after entering the voting logic processing, it will jump to the above voting result function and return the voting result to the user. Among them, polls: results is a route that points to the results function above. If you don't understand this place, please check the documentation yourself, which is not within the scope of this article. What this article is going to discuss is how to pass the actual parameters of the question_id parameter when redirecting to the results function. There is an args=[question_id] in the return statement of the vote function, which is to pass parameters in the redirection. It is a list, so if there are multiple parameters in the results function, please pass them to args in order.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325847847&siteId=291194637