重定向到另一个视图功能

时间:2016-09-14 17:39:05

标签: django

当用户登录时,我想将它们重定向到索引,而不是使用' / login /'在网址中。

我有两个用于登录和索引的视图功能。

索引视图

public function find($invoice_id = null) {
        if($invoice_id) {
            $data = $this->_db->query("SELECT * FROM invoice WHERE invoice_id =" . $invoice_id . "");   

            if($data->row_count()) {
                $this->_data = $data->first();
                return true;
            }
        }
        return false;
    }

    public function data() {
        return $this->_data;
    }

登录视图

def index(request):

    context = {

    }
    return render(request, 'index.html', context)

网址格式

def login(request):

    username = request.POST.get('username', '')
    password = request.POST.get('password', '')

    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
    else:
        print('Wrong user') 

    return redirect('index') #<------ Something like that(?)

控制台错误:

urlpatterns = [
            url(r'^$', views.index, name='index'),

            url(r'^login/$', views.login, name='login'),      
]

1 个答案:

答案 0 :(得分:4)

# views.py
def login(...):
    # ...
    return redirect('home')

# urls.py
urlpatterns = [
    url(r'^$', 'views.index', name='home'),
    # ...
]