2017-02-21 81 views
1

我有一个认证用户的视图。如果用户通过身份验证,程序应该以请求和用户作为参数调用另一个视图。是否有必要在Django中实例化新的HttpRequest对象?

def home(request): 
     if request.method == "POST": 
      username = request.POST.get('username') 
      password = request.POST.get('password') 
      user = authenticate(username=username, password=password) 

      if user is not None: 
       return index(request, user) 
      else: 
       context = {'error_message': "That username and password don't exist in our system."} 
       return render(request, 'list/home.html', context) 

因此,当索引被调用时,请求是与请求发送到主视图相同的实例,对吧?我担心的是请求仍然是一个POST请求,它应该是一个GET请求。

这是一种误解吗?我应该创建一个新的请求对象并将其发送到索引?

谢谢。

回答

2

您应该使用redirect方法

if user is not None: 
    return redirect(reverse('index'))